URLExtract

URLExtract

const URLExtract = require('paraviewweb/src/Common/Misc/URLExtract');

toNativeType(str)

> toNativeType('')
""
> toNativeType(' ')
NaN
> toNativeType(' 4')
4
> toNativeType(' d ')
" d "
> toNativeType(' d')
" d"
> toNativeType(NaN)
NaN
> toNativeType('NaN')
"NaN"
> toNativeType('4.6.7.8')
"4.6.7.8"
> toNativeType('4.678')
4.678
> toNativeType(' 4.678 ')
4.678
> toNativeType(' 4.67 8 ')
" 4.67 8 "

extractURLParameters(castToNativeType = true, query = window.location.search)

const query = '?a=b&c=d%20d&d=5.6&e=true&f=undefined&g';
const strObj = ;
const typeObj = extractURLParameters(true, query);

console.log(extractURLParameters(false, query));
{
a: 'b',
c: 'd d',
d: '5.6',
e: 'true',
f: 'undefined',
g: true
}

console.log(extractURLParameters(true, query));
{
a: 'b',
c: 'd d',
d: 5.6,
e: true,
f: undefined,
g: true
}

Source

index.js
/* eslint-env browser */

function identity(i) {
return i;
}

function toNativeType(str) {
if (str === null || str === 'null') {
return null;
} else if (str === 'true') {
return true;
} else if (str === 'false') {
return false;
} else if (str === undefined || str === 'undefined') {
return undefined;
} else if (str === '' || Number.isNaN(Number(str))) {
return str;
}
return parseFloat(str);
}

function extractURLParameters(
castToNativeType = true,
query = window.location.search
) {
const summary = {};
const convert = castToNativeType ? toNativeType : identity;
const queryTokens = (query || '')
.replace(/#.*/, '') // remove hash query
.replace('?', '') // Remove ? from the head
.split('&'); // extract token pair

queryTokens.forEach((token) => {
const [key, value] = token.split('=').map((s) => decodeURIComponent(s));
if (key) {
summary[key] = value ? convert(value) : true;
}
});

return summary;
}

export default {
toNativeType,
extractURLParameters,
};