pax_global_header00006660000000000000000000000064126601116160014512gustar00rootroot0000000000000052 comment=4e103efe352cfcb072366e3f43aa355c0932bace LinusU-node-append-field-92e46f1/000077500000000000000000000000001266011161600165445ustar00rootroot00000000000000LinusU-node-append-field-92e46f1/.gitignore000066400000000000000000000000161266011161600205310ustar00rootroot00000000000000node_modules/ LinusU-node-append-field-92e46f1/LICENSE000066400000000000000000000020721266011161600175520ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2015 Linus Unnebäck Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. LinusU-node-append-field-92e46f1/README.md000066400000000000000000000017701266011161600200300ustar00rootroot00000000000000# `append-field` A [W3C HTML JSON forms spec](http://www.w3.org/TR/html-json-forms/) compliant field appender (for lack of a better name). Useful for people implementing `application/x-www-form-urlencoded` and `multipart/form-data` parsers. It works best on objects created with `Object.create(null)`. Otherwise it might conflict with variables from the prototype (e.g. `hasOwnProperty`). ## Installation ```sh npm install --save append-field ``` ## Usage ```javascript var appendField = require('append-field') var obj = Object.create(null) appendField(obj, 'pets[0][species]', 'Dahut') appendField(obj, 'pets[0][name]', 'Hypatia') appendField(obj, 'pets[1][species]', 'Felis Stultus') appendField(obj, 'pets[1][name]', 'Billie') console.log(obj) ``` ```text { pets: [ { species: 'Dahut', name: 'Hypatia' }, { species: 'Felis Stultus', name: 'Billie' } ] } ``` ## API ### `appendField(store, key, value)` Adds the field named `key` with the value `value` to the object `store`. ## License MIT LinusU-node-append-field-92e46f1/index.js000066400000000000000000000004631266011161600202140ustar00rootroot00000000000000var parsePath = require('./lib/parse-path') var setValue = require('./lib/set-value') function appendField (store, key, value) { var steps = parsePath(key) steps.reduce(function (context, step) { return setValue(context, step, context[step.key], value) }, store) } module.exports = appendField LinusU-node-append-field-92e46f1/lib/000077500000000000000000000000001266011161600173125ustar00rootroot00000000000000LinusU-node-append-field-92e46f1/lib/parse-path.js000066400000000000000000000021301266011161600217100ustar00rootroot00000000000000var reFirstKey = /^[^\[]*/ var reDigitPath = /^\[(\d+)\]/ var reNormalPath = /^\[([^\]]+)\]/ function parsePath (key) { function failure () { return [{ type: 'object', key: key, last: true }] } var 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] while (pos < len) { var m if (key[pos] === '[' && key[pos + 1] === ']') { pos += 2 tail.append = true if (pos !== len) return failure() continue } m = reDigitPath.exec(key.substring(pos)) if (m !== null) { pos += m[0].length tail.nextType = 'array' tail = { type: 'array', key: parseInt(m[1], 10) } steps.push(tail) continue } m = reNormalPath.exec(key.substring(pos)) if (m !== null) { pos += m[0].length tail.nextType = 'object' tail = { type: 'object', key: m[1] } steps.push(tail) continue } return failure() } tail.last = true return steps } module.exports = parsePath LinusU-node-append-field-92e46f1/lib/set-value.js000066400000000000000000000031521266011161600215560ustar00rootroot00000000000000function valueType (value) { if (value === undefined) return 'undefined' if (Array.isArray(value)) return 'array' if (typeof value === 'object') return 'object' return 'scalar' } function setLastValue (context, step, currentValue, entryValue) { switch (valueType(currentValue)) { case 'undefined': if (step.append) { context[step.key] = [entryValue] } else { context[step.key] = entryValue } break case 'array': context[step.key].push(entryValue) break case 'object': return setLastValue(currentValue, { type: 'object', key: '', last: true }, currentValue[''], entryValue) case 'scalar': context[step.key] = [context[step.key], entryValue] break } return context } function setValue (context, step, currentValue, entryValue) { if (step.last) return setLastValue(context, step, currentValue, entryValue) var obj switch (valueType(currentValue)) { case 'undefined': if (step.nextType === 'array') { context[step.key] = [] } else { context[step.key] = Object.create(null) } return context[step.key] case 'object': return context[step.key] case 'array': if (step.nextType === 'array') { return currentValue } obj = Object.create(null) context[step.key] = obj currentValue.forEach(function (item, i) { if (item !== undefined) obj['' + i] = item }) return obj case 'scalar': obj = Object.create(null) obj[''] = currentValue context[step.key] = obj return obj } } module.exports = setValue LinusU-node-append-field-92e46f1/package.json000066400000000000000000000006401266011161600210320ustar00rootroot00000000000000{ "name": "append-field", "version": "1.0.0", "license": "MIT", "author": "Linus Unnebäck ", "main": "index.js", "devDependencies": { "mocha": "^2.2.4", "standard": "^6.0.5", "testdata-w3c-json-form": "^0.2.0" }, "scripts": { "test": "standard && mocha" }, "repository": { "type": "git", "url": "http://github.com/LinusU/node-append-field.git" } } LinusU-node-append-field-92e46f1/test/000077500000000000000000000000001266011161600175235ustar00rootroot00000000000000LinusU-node-append-field-92e46f1/test/forms.js000066400000000000000000000007021266011161600212060ustar00rootroot00000000000000/* eslint-env mocha */ var assert = require('assert') var appendField = require('../') var testData = require('testdata-w3c-json-form') describe('Append Field', function () { for (var test of testData) { it('handles ' + test.name, function () { var store = Object.create(null) for (var field of test.fields) { appendField(store, field.key, field.value) } assert.deepEqual(store, test.expected) }) } })