Comparing version 1.0.1 to 1.1.0
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 | ||
[![npm version](https://img.shields.io/npm/v/nvp-json.svg)](https://www.npmjs.com/package/nvp-json) [![Build Status](https://img.shields.io/travis/carriejv/nvp-json-js.svg)](https://travis-ci.org/carriejv/nvp-json-js) [![dependencies](https://img.shields.io/david/carriejv/nvp-json-js.svg)](https://david-dm.org/carriejv/nvp-json-js) [![devDependencies](https://img.shields.io/david/dev/carriejv/nvp-json-js.svg)](https://david-dm.org/carriejv/nvp-json-js#info=devDependencies) | ||
[![npm version](https://img.shields.io/npm/v/nvp-json.svg)](https://www.npmjs.com/package/nvp-json) [![Build Status](https://img.shields.io/travis/carriejv/nvp-json-js.svg)](https://travis-ci.org/carriejv/nvp-json-js) [![Codacy Badge](https://api.codacy.com/project/badge/Grade/9065cebcf7384259bac3bce3d502d97e)](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) [![Code Coverage](https://img.shields.io/codacy/coverage/nvp-json-js.svg)](https://app.codacy.com/app/carriejv/nvp-json-js) | ||
[![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://www.paypal.me/carriejv) [![GitKraken](https://img.shields.io/badge/<3-GitKraken-green.svg)](https://www.gitkraken.com/invite/om4Du5zG) [![dependencies](https://img.shields.io/david/carriejv/nvp-json-js.svg)](https://david-dm.org/carriejv/nvp-json-js) [![devDependencies](https://img.shields.io/david/dev/carriejv/nvp-json-js.svg)](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 |
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
7557
8
117
44
11