Comparing version 0.9.1 to 1.0.3
97
index.js
@@ -1,4 +0,95 @@ | ||
'use strict' | ||
exports.decode = exports.parse = require('./decode') | ||
exports.encode = exports.stringify = require('./encode') | ||
module.exports = Harakee = { | ||
toHex: (str) => { | ||
let hex = '' | ||
for (var i = 0; i < str.length; i++) | ||
hex += ''+str.charCodeAt(i).toString(16) | ||
return hex | ||
}, | ||
fromHex: (hex) => { | ||
hex = hex.toString(), str = '' | ||
for (var i = 0; i < hex.length; i += 2) | ||
str += String.fromCharCode(parseInt(hex.substr(i, 2), 16)) | ||
return str | ||
}, | ||
encode: (obj, sep, eq, name) => { | ||
sep = sep || '&' | ||
eq = eq || '=' | ||
if (obj === null){ obj = undefined } | ||
if (typeof obj === 'object'){ | ||
return Object.keys(obj).map((k) => { | ||
let ks = encodeURIComponent(stringify(k)) + eq | ||
if (Array.isArray(obj[k])){ | ||
return obj[k].map((v) => { | ||
return ks + encodeURIComponent(stringify(v)) | ||
}).join(sep) | ||
} else { | ||
return ks + encodeURIComponent(stringify(obj[k])) | ||
} | ||
}).join(sep) | ||
} | ||
if (!name) return '' | ||
return encodeURIComponent(stringify(name)) + eq + encodeURIComponent(stringify(obj)) | ||
}, | ||
decode: (qs, sep, eq, options) => { | ||
sep = sep || '&' | ||
eq = eq || '=' | ||
let obj = {} | ||
if (typeof qs !== 'string' || qs.length === 0){ | ||
return obj | ||
} | ||
qs = qs.split(sep) | ||
let maxKeys = 1000 | ||
if (options && typeof options.maxKeys === 'number'){ | ||
maxKeys = options.maxKeys | ||
} | ||
let len = qs.length | ||
if (maxKeys > 0 && len > maxKeys){ | ||
len = maxKeys | ||
} | ||
for (let i = 0; i < len; ++i){ | ||
let x = qs[i].replace(regexp, '%20'), | ||
idx = x.indexOf(eq), | ||
kstr, vstr, k, v | ||
if (idx >= 0){ | ||
kstr = x.substr(0, idx) | ||
vstr = x.substr(idx + 1) | ||
} else { | ||
kstr = x | ||
vstr = '' | ||
} | ||
k = decodeURIComponent(kstr) | ||
v = decodeURIComponent(vstr) | ||
if (!hasOwnProperty(obj, k)){ | ||
obj[k] = v | ||
} else if (Array.isArray(obj[k])){ | ||
obj[k].push(v) | ||
} else { | ||
obj[k] = [obj[k], v] | ||
} | ||
} | ||
return obj | ||
} | ||
} | ||
let | ||
stringify = (v) => { | ||
switch (typeof v){ | ||
case 'string': | ||
return v | ||
case 'boolean': | ||
return v ? 'true' : 'false' | ||
case 'number': | ||
return isFinite(v) ? v : '' | ||
default: | ||
return '' | ||
} | ||
}, | ||
hasOwnProperty = (obj, prop) => { | ||
return Object.prototype.hasOwnProperty.call(obj, prop) | ||
} |
{ | ||
"name": "harakee", | ||
"version": "0.9.1", | ||
"version": "1.0.3", | ||
"description": "Utilities for parsing and formatting query strings.", | ||
@@ -11,2 +11,4 @@ "main": "switch.js", | ||
"keywords": [ | ||
"string", | ||
"hex", | ||
"query", | ||
@@ -13,0 +15,0 @@ "formatting", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
5736
163
1