UNPKG

@moomfe/zenjs

Version:

ZenJS 是一个高性能的 JavaScript 工具库。

68 lines (53 loc) 1.79 kB
import parametersDefault from "../../../shared/util/parametersDefault"; import isObject from "../../../shared/util/isObject"; import keys from "../../../shared/global/Object/keys"; import isString from "../../../shared/util/isString"; import defineValue from "../../../shared/util/defineValue"; import root from "../../../shared/const/root"; import assign from "../../../shared/util/assign"; const rBackSlant = /\+/g; function toString( obj ){ switch( typeof obj ){ case 'string': return obj; case 'boolean': return obj ? 'true' : 'false'; case 'number': return isFinite( obj ) ? obj : ''; default: return ''; } } export function stringify( obj ){ const args = arguments; const sep = parametersDefault( args, 1, '&' ); const eq = parametersDefault( args, 2, '=' ); if( isObject( obj ) ){ return keys( obj ).map( key => { return encodeURIComponent( toString( key ) ) + eq + encodeURIComponent( toString( obj[ key ] ) ); }) .join( sep ); } return ''; } export function parse( str ){ const result = {}; if( !str || !isString( str ) ){ return result; } const args = arguments; const sep = parametersDefault( args, 1, '&' ); const eq = parametersDefault( args, 2, '=' ); str.split( sep ).forEach( _value => { const cache = _value.replace( rBackSlant, '%20' ); const index = cache.indexOf( eq ); let key, value = ''; if( index > -1 ){ key = cache.substr( 0, index ); value = cache.substr( index + 1 ); }else{ key = cache; } result[ decodeURIComponent( key ) ] = decodeURIComponent( value ); }); return result; } defineValue( root, '$querystring', assign( false, [ null, { stringify, parse } ]) );