Comparing version
79
index.js
@@ -7,3 +7,2 @@ /*! | ||
'use strict'; | ||
@@ -17,10 +16,10 @@ /** | ||
*/ | ||
let toNVP = function(data) { | ||
let json = JSON.parse(JSON.stringify(data)); | ||
let res = ''; | ||
for(let k of Object.keys(json)) { | ||
res += `${(res ? '&' : '')}${k.toUpperCase()}${json[k].includes('&') | json[k].includes('=') ? `[${json[k].length}]` : ''}=${json[k]}`; | ||
} | ||
return res; | ||
} | ||
const toNVP = function (data) { | ||
const json = JSON.parse(JSON.stringify(data)); | ||
let res = ''; | ||
for (const k of Object.keys(json)) { | ||
res += `${(res ? '&' : '')}${k.toUpperCase()}${json[k].includes('&') | json[k].includes('=') ? `[${json[k].length}]` : ''}=${json[k]}`; | ||
} | ||
return res; | ||
}; | ||
@@ -34,36 +33,36 @@ /** | ||
*/ | ||
let toJSON = function(data) { | ||
let res = {}; | ||
let pairs = []; | ||
let ignore = {}; | ||
let search; | ||
let regexSearchIndex = 0; | ||
while((search = data.slice(regexSearchIndex).search(/\[[0-9]+\]=/)) !== -1) { | ||
let val = parseInt(data.substring(search + regexSearchIndex + 1, data.indexOf(']', search + regexSearchIndex))); | ||
ignore[search + regexSearchIndex] = val; | ||
data = data.slice(0, regexSearchIndex) + data.slice(regexSearchIndex).replace(/\[[0-9]+\]=/, '='); // Remove first remaining (current) instance from data. | ||
regexSearchIndex += search + val; | ||
const toJSON = function (data) { | ||
const res = {}; | ||
const pairs = []; | ||
const ignore = {}; | ||
let search; | ||
let regexSearchIndex = 0; | ||
while ((search = data.slice(regexSearchIndex).search(/\[[0-9]+\]=/)) !== -1) { | ||
const val = parseInt(data.substring(search + regexSearchIndex + 1, data.indexOf(']', search + regexSearchIndex))); | ||
ignore[search + regexSearchIndex] = val; | ||
data = data.slice(0, regexSearchIndex) + data.slice(regexSearchIndex).replace(/\[[0-9]+\]=/, '='); // Remove first remaining (current) instance from data. | ||
regexSearchIndex += search + val; | ||
} | ||
let lastPair = -1; | ||
for (let i = 0; i < data.length; i++) { | ||
if (ignore[i]) { | ||
i += ignore[i]; | ||
continue; | ||
} | ||
var lastPair = -1; | ||
for(let i = 0; i < data.length; i++) { | ||
if(ignore[i]) { | ||
i += ignore[i]; | ||
continue; | ||
} | ||
if(data.charAt(i) === '&') { | ||
pairs.push(data.substring(lastPair + 1, i)); | ||
lastPair = i; | ||
} | ||
if (data.charAt(i) === '&') { | ||
pairs.push(data.substring(lastPair + 1, i)); | ||
lastPair = i; | ||
} | ||
pairs.push(data.substring(lastPair + 1)); // Add in last pair. | ||
for(let i = 0; i < pairs.length; i++) { | ||
let pair = pairs[i].split(/=(.+)/); | ||
res[pair[0]] = pair[1]; | ||
} | ||
return res; | ||
} | ||
} | ||
pairs.push(data.substring(lastPair + 1)); // Add in last pair. | ||
for (let i = 0; i < pairs.length; i++) { | ||
const pair = pairs[i].split(/=(.+)/); | ||
res[pair[0]] = pair[1]; | ||
} | ||
return res; | ||
}; | ||
module.exports = { | ||
toNVP: toNVP, | ||
toJSON: toJSON | ||
}; | ||
toNVP, | ||
toJSON, | ||
}; |
{ | ||
"name": "nvp-json", | ||
"version": "1.0.1", | ||
"description": "A simple bidirectional NVP/JSON parser for use with PayPal NVP APIs.", | ||
"version": "1.1.0", | ||
"description": "A bidirectional NVP/JSON parser for use with PayPal NVP APIs.", | ||
"main": "index.js", | ||
@@ -11,3 +11,6 @@ "directories": { | ||
"start": "node index.js", | ||
"test": "nyc mocha --exit" | ||
"test": "istanbul cover _mocha", | ||
"test-with-coverage": "istanbul cover _mocha --report lcovonly -- -R spec && cat ./coverage/lcov.info | codacy-coverage && rm -rf ./coverage", | ||
"lint": "./node_modules/.bin/eslint .", | ||
"lint-fix": "./node_modules/.bin/eslint --fix ." | ||
}, | ||
@@ -22,5 +25,5 @@ "repository": { | ||
"homepage": "https://github.com/carriejv/nvp-json-js", | ||
"author": "Carrie Vrtis <cjvrtis@gmail.com> (https://www.carrievrtis.com)", | ||
"author": "Carrie Vrtis <carrie@carriejv.com> (https://www.carrievrtis.com)", | ||
"contributors": [ | ||
"Carrie Vrtis <cjvrtis@gmail.com> (https://www.carrievrtis.com)" | ||
"Carrie Vrtis <carrie@carriejv.com> (https://www.carrievrtis.com)" | ||
], | ||
@@ -41,6 +44,13 @@ "keywords": [ | ||
"chai": "^4.2.0", | ||
"codacy-coverage": "^3.4.0", | ||
"eslint": "^5.15.3", | ||
"eslint-config-airbnb-base": "^13.1.0", | ||
"eslint-plugin-import": "^2.16.0", | ||
"eslint-plugin-node": "^8.0.1", | ||
"eslint-plugin-promise": "^4.0.1", | ||
"eslint-plugin-standard": "^4.0.0", | ||
"istanbul": "^0.4.5", | ||
"mocha": "^5.2.0", | ||
"nyc": "^13.1.0" | ||
"mocha-lcov-reporter": "^1.3.0" | ||
} | ||
} |
# nvp-json | ||
[](https://www.npmjs.com/package/nvp-json) [](https://travis-ci.org/carriejv/nvp-json-js) [](https://david-dm.org/carriejv/nvp-json-js) [](https://david-dm.org/carriejv/nvp-json-js#info=devDependencies) | ||
[](https://www.npmjs.com/package/nvp-json) [](https://travis-ci.org/carriejv/nvp-json-js) [](https://www.codacy.com/app/carriejv/nvp-json-js?utm_source=github.com&utm_medium=referral&utm_content=carriejv/nvp-json-js&utm_campaign=Badge_Grade) [](https://app.codacy.com/app/carriejv/nvp-json-js) | ||
[](https://www.paypal.me/carriejv) [](https://www.gitkraken.com/invite/om4Du5zG) [](https://david-dm.org/carriejv/nvp-json-js) [](https://david-dm.org/carriejv/nvp-json-js#info=devDependencies) | ||
A bi-directional NVP-JSON parser, for use with [PayPal's NVP APIs](https://developer.paypal.com/reference/?mark=endpoints). | ||
Tested with Node `6.0+`. | ||
Should work with prior versions, but will not test correctly due to nyc dependencies. | ||
@@ -11,0 +12,0 @@ ## Installation |
7557
21.93%8
33.33%117
32.95%44
2.33%11
175%