append-field
Advanced tools
Comparing version 1.0.0 to 2.0.0
12
index.js
@@ -1,12 +0,10 @@ | ||
var parsePath = require('./lib/parse-path') | ||
var setValue = require('./lib/set-value') | ||
import parsePath from './lib/parse-path.js' | ||
import setValue from './lib/set-value.js' | ||
function appendField (store, key, value) { | ||
var steps = parsePath(key) | ||
export default function appendField (store, key, value) { | ||
const steps = parsePath(key) | ||
steps.reduce(function (context, step) { | ||
steps.reduce((context, step) => { | ||
return setValue(context, step, context[step.key], value) | ||
}, store) | ||
} | ||
module.exports = appendField |
@@ -1,20 +0,20 @@ | ||
var reFirstKey = /^[^\[]*/ | ||
var reDigitPath = /^\[(\d+)\]/ | ||
var reNormalPath = /^\[([^\]]+)\]/ | ||
const reFirstKey = /^[^[]*/ | ||
const reDigitPath = /^\[(\d+)\]/ | ||
const reNormalPath = /^\[([^\]]+)\]/ | ||
function parsePath (key) { | ||
export default function parsePath (key) { | ||
function failure () { | ||
return [{ type: 'object', key: key, last: true }] | ||
return [{ type: 'object', key, last: true }] | ||
} | ||
var firstKey = reFirstKey.exec(key)[0] | ||
const firstKey = reFirstKey.exec(key)[0] | ||
if (!firstKey) return failure() | ||
var len = key.length | ||
var pos = firstKey.length | ||
var tail = { type: 'object', key: firstKey } | ||
var steps = [tail] | ||
const len = key.length | ||
let pos = firstKey.length | ||
let tail = { type: 'object', key: firstKey } | ||
const steps = [tail] | ||
while (pos < len) { | ||
var m | ||
let m | ||
@@ -52,3 +52,1 @@ if (key[pos] === '[' && key[pos + 1] === ']') { | ||
} | ||
module.exports = parsePath |
@@ -30,8 +30,7 @@ function valueType (value) { | ||
function setValue (context, step, currentValue, entryValue) { | ||
export default function setValue (context, step, currentValue, entryValue) { | ||
if (step.last) return setLastValue(context, step, currentValue, entryValue) | ||
var obj | ||
switch (valueType(currentValue)) { | ||
case 'undefined': | ||
case 'undefined': { | ||
if (step.nextType === 'array') { | ||
@@ -42,6 +41,11 @@ context[step.key] = [] | ||
} | ||
return context[step.key] | ||
case 'object': | ||
} | ||
case 'object': { | ||
return context[step.key] | ||
case 'array': | ||
} | ||
case 'array': { | ||
if (step.nextType === 'array') { | ||
@@ -51,3 +55,3 @@ return currentValue | ||
obj = Object.create(null) | ||
const obj = Object.create(null) | ||
context[step.key] = obj | ||
@@ -59,10 +63,11 @@ currentValue.forEach(function (item, i) { | ||
return obj | ||
case 'scalar': | ||
obj = Object.create(null) | ||
} | ||
case 'scalar': { | ||
const obj = Object.create(null) | ||
obj[''] = currentValue | ||
context[step.key] = obj | ||
return obj | ||
} | ||
} | ||
} | ||
module.exports = setValue |
{ | ||
"name": "append-field", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"license": "MIT", | ||
"author": "Linus Unnebäck <linus@folkdatorn.se>", | ||
"main": "index.js", | ||
"repository": "LinusU/node-append-field", | ||
"type": "module", | ||
"exports": "./index.js", | ||
"scripts": { | ||
"test": "standard && mocha && ts-readme-generator --check" | ||
}, | ||
"devDependencies": { | ||
"mocha": "^2.2.4", | ||
"standard": "^6.0.5", | ||
"testdata-w3c-json-form": "^0.2.0" | ||
"mocha": "^8.4.0", | ||
"standard": "^16.0.3", | ||
"testdata-w3c-json-form": "^0.2.0", | ||
"ts-readme-generator": "^0.5.2" | ||
}, | ||
"scripts": { | ||
"test": "standard && mocha" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "http://github.com/LinusU/node-append-field.git" | ||
"engines": { | ||
"node": "^12.20.0 || ^14.13.1 || >=16.0.0" | ||
} | ||
} |
/* eslint-env mocha */ | ||
var assert = require('assert') | ||
var appendField = require('../') | ||
var testData = require('testdata-w3c-json-form') | ||
import assert from 'node:assert' | ||
import testData from 'testdata-w3c-json-form' | ||
import appendField from '../index.js' | ||
describe('Append Field', function () { | ||
for (var test of testData) { | ||
it('handles ' + test.name, function () { | ||
var store = Object.create(null) | ||
describe('Append Field', () => { | ||
for (const test of testData) { | ||
it(`handles ${test.name}`, () => { | ||
const store = Object.create(null) | ||
for (var field of test.fields) { | ||
for (const field of test.fields) { | ||
appendField(store, field.key, field.value) | ||
@@ -14,0 +14,0 @@ } |
Sorry, the diff of this file is not supported yet
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 contributors or author data
MaintenancePackage does not specify a list of contributors or an author in package.json.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
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
6819
9
126
50
Yes
4
2
1