476 lines
14 KiB
JavaScript
476 lines
14 KiB
JavaScript
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.jsonSpread = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
|
|
var isBuffer = require('is-buffer')
|
|
|
|
var flat = module.exports = flatten
|
|
flatten.flatten = flatten
|
|
flatten.unflatten = unflatten
|
|
|
|
function flatten(target, opts) {
|
|
opts = opts || {}
|
|
|
|
var delimiter = opts.delimiter || '.'
|
|
var maxDepth = opts.maxDepth
|
|
var currentDepth = 1
|
|
var output = {}
|
|
|
|
function step(object, prev) {
|
|
Object.keys(object).forEach(function(key) {
|
|
var value = object[key]
|
|
var isarray = opts.safe && Array.isArray(value)
|
|
var type = Object.prototype.toString.call(value)
|
|
var isbuffer = isBuffer(value)
|
|
var isobject = (
|
|
type === "[object Object]" ||
|
|
type === "[object Array]"
|
|
)
|
|
|
|
var newKey = prev
|
|
? prev + delimiter + key
|
|
: key
|
|
|
|
if (!opts.maxDepth) {
|
|
maxDepth = currentDepth + 1;
|
|
}
|
|
|
|
if (!isarray && !isbuffer && isobject && Object.keys(value).length && currentDepth < maxDepth) {
|
|
++currentDepth
|
|
return step(value, newKey)
|
|
}
|
|
|
|
output[newKey] = value
|
|
})
|
|
}
|
|
|
|
step(target)
|
|
|
|
return output
|
|
}
|
|
|
|
function unflatten(target, opts) {
|
|
opts = opts || {}
|
|
|
|
var delimiter = opts.delimiter || '.'
|
|
var overwrite = opts.overwrite || false
|
|
var result = {}
|
|
|
|
var isbuffer = isBuffer(target)
|
|
if (isbuffer || Object.prototype.toString.call(target) !== '[object Object]') {
|
|
return target
|
|
}
|
|
|
|
// safely ensure that the key is
|
|
// an integer.
|
|
function getkey(key) {
|
|
var parsedKey = Number(key)
|
|
|
|
return (
|
|
isNaN(parsedKey) ||
|
|
key.indexOf('.') !== -1
|
|
) ? key
|
|
: parsedKey
|
|
}
|
|
|
|
Object.keys(target).forEach(function(key) {
|
|
var split = key.split(delimiter)
|
|
var key1 = getkey(split.shift())
|
|
var key2 = getkey(split[0])
|
|
var recipient = result
|
|
|
|
while (key2 !== undefined) {
|
|
var type = Object.prototype.toString.call(recipient[key1])
|
|
var isobject = (
|
|
type === "[object Object]" ||
|
|
type === "[object Array]"
|
|
)
|
|
|
|
// do not write over falsey, non-undefined values if overwrite is false
|
|
if (!overwrite && !isobject && typeof recipient[key1] !== 'undefined') {
|
|
return
|
|
}
|
|
|
|
if ((overwrite && !isobject) || (!overwrite && recipient[key1] == null)) {
|
|
recipient[key1] = (
|
|
typeof key2 === 'number' &&
|
|
!opts.object ? [] : {}
|
|
)
|
|
}
|
|
|
|
recipient = recipient[key1]
|
|
if (split.length > 0) {
|
|
key1 = getkey(split.shift())
|
|
key2 = getkey(split[0])
|
|
}
|
|
}
|
|
|
|
// unflatten again for 'messy objects'
|
|
recipient[key1] = unflatten(target[key], opts)
|
|
})
|
|
|
|
return result
|
|
}
|
|
|
|
},{"is-buffer":4}],2:[function(require,module,exports){
|
|
"use strict";
|
|
|
|
module.exports = function(arr, iter, context) {
|
|
var results = [];
|
|
if (!Array.isArray(arr)) return results;
|
|
arr.forEach(function(value, index, list) {
|
|
var res = iter.call(context, value, index, list);
|
|
if (Array.isArray(res)) {
|
|
results.push.apply(results, res);
|
|
} else if (res != null) {
|
|
results.push(res);
|
|
}
|
|
});
|
|
return results;
|
|
};
|
|
},{}],3:[function(require,module,exports){
|
|
|
|
var hasOwn = Object.prototype.hasOwnProperty;
|
|
var toString = Object.prototype.toString;
|
|
|
|
module.exports = function forEach (obj, fn, ctx) {
|
|
if (toString.call(fn) !== '[object Function]') {
|
|
throw new TypeError('iterator must be a function');
|
|
}
|
|
var l = obj.length;
|
|
if (l === +l) {
|
|
for (var i = 0; i < l; i++) {
|
|
fn.call(ctx, obj[i], i, obj);
|
|
}
|
|
} else {
|
|
for (var k in obj) {
|
|
if (hasOwn.call(obj, k)) {
|
|
fn.call(ctx, obj[k], k, obj);
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
|
|
},{}],4:[function(require,module,exports){
|
|
/**
|
|
* Determine if an object is Buffer
|
|
*
|
|
* Author: Feross Aboukhadijeh <feross@feross.org> <http://feross.org>
|
|
* License: MIT
|
|
*
|
|
* `npm install is-buffer`
|
|
*/
|
|
|
|
module.exports = function (obj) {
|
|
return !!(obj != null &&
|
|
(obj._isBuffer || // For Safari 5-7 (missing Object.prototype.constructor)
|
|
(obj.constructor &&
|
|
typeof obj.constructor.isBuffer === 'function' &&
|
|
obj.constructor.isBuffer(obj))
|
|
))
|
|
}
|
|
|
|
},{}],5:[function(require,module,exports){
|
|
/*!
|
|
* is-plain-object <https://github.com/jonschlinkert/is-plain-object>
|
|
*
|
|
* Copyright (c) 2014-2015, Jon Schlinkert.
|
|
* Licensed under the MIT License.
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
var isObject = require('isobject');
|
|
|
|
function isObjectObject(o) {
|
|
return isObject(o) === true
|
|
&& Object.prototype.toString.call(o) === '[object Object]';
|
|
}
|
|
|
|
module.exports = function isPlainObject(o) {
|
|
var ctor,prot;
|
|
|
|
if (isObjectObject(o) === false) return false;
|
|
|
|
// If has modified constructor
|
|
ctor = o.constructor;
|
|
if (typeof ctor !== 'function') return false;
|
|
|
|
// If has modified prototype
|
|
prot = ctor.prototype;
|
|
if (isObjectObject(prot) === false) return false;
|
|
|
|
// If constructor does not have an Object-specific method
|
|
if (prot.hasOwnProperty('isPrototypeOf') === false) {
|
|
return false;
|
|
}
|
|
|
|
// Most likely a plain Object
|
|
return true;
|
|
};
|
|
|
|
},{"isobject":6}],6:[function(require,module,exports){
|
|
/*!
|
|
* isobject <https://github.com/jonschlinkert/isobject>
|
|
*
|
|
* Copyright (c) 2014-2015, Jon Schlinkert.
|
|
* Licensed under the MIT License.
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
module.exports = function isObject(val) {
|
|
return val != null && typeof val === 'object'
|
|
&& !Array.isArray(val);
|
|
};
|
|
|
|
},{}],7:[function(require,module,exports){
|
|
'use strict';
|
|
/* eslint-disable no-unused-vars */
|
|
var hasOwnProperty = Object.prototype.hasOwnProperty;
|
|
var propIsEnumerable = Object.prototype.propertyIsEnumerable;
|
|
|
|
function toObject(val) {
|
|
if (val === null || val === undefined) {
|
|
throw new TypeError('Object.assign cannot be called with null or undefined');
|
|
}
|
|
|
|
return Object(val);
|
|
}
|
|
|
|
function shouldUseNative() {
|
|
try {
|
|
if (!Object.assign) {
|
|
return false;
|
|
}
|
|
|
|
// Detect buggy property enumeration order in older V8 versions.
|
|
|
|
// https://bugs.chromium.org/p/v8/issues/detail?id=4118
|
|
var test1 = new String('abc'); // eslint-disable-line
|
|
test1[5] = 'de';
|
|
if (Object.getOwnPropertyNames(test1)[0] === '5') {
|
|
return false;
|
|
}
|
|
|
|
// https://bugs.chromium.org/p/v8/issues/detail?id=3056
|
|
var test2 = {};
|
|
for (var i = 0; i < 10; i++) {
|
|
test2['_' + String.fromCharCode(i)] = i;
|
|
}
|
|
var order2 = Object.getOwnPropertyNames(test2).map(function (n) {
|
|
return test2[n];
|
|
});
|
|
if (order2.join('') !== '0123456789') {
|
|
return false;
|
|
}
|
|
|
|
// https://bugs.chromium.org/p/v8/issues/detail?id=3056
|
|
var test3 = {};
|
|
'abcdefghijklmnopqrst'.split('').forEach(function (letter) {
|
|
test3[letter] = letter;
|
|
});
|
|
if (Object.keys(Object.assign({}, test3)).join('') !==
|
|
'abcdefghijklmnopqrst') {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
} catch (e) {
|
|
// We don't expect any of the above to throw, but better to be safe.
|
|
return false;
|
|
}
|
|
}
|
|
|
|
module.exports = shouldUseNative() ? Object.assign : function (target, source) {
|
|
var from;
|
|
var to = toObject(target);
|
|
var symbols;
|
|
|
|
for (var s = 1; s < arguments.length; s++) {
|
|
from = Object(arguments[s]);
|
|
|
|
for (var key in from) {
|
|
if (hasOwnProperty.call(from, key)) {
|
|
to[key] = from[key];
|
|
}
|
|
}
|
|
|
|
if (Object.getOwnPropertySymbols) {
|
|
symbols = Object.getOwnPropertySymbols(from);
|
|
for (var i = 0; i < symbols.length; i++) {
|
|
if (propIsEnumerable.call(from, symbols[i])) {
|
|
to[symbols[i]] = from[symbols[i]];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return to;
|
|
};
|
|
|
|
},{}],8:[function(require,module,exports){
|
|
/*
|
|
*The MIT License (MIT)
|
|
*Copyright (c) 2016 Xun Chen
|
|
*
|
|
*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.
|
|
*/
|
|
var spread = require('./spread.js');
|
|
var assign = require('object-assign');
|
|
|
|
var defaultOptions = {
|
|
delimiter : ".",
|
|
removeEmptyArray : false,
|
|
emptyValue : null,
|
|
safe : true,
|
|
debug : false
|
|
}
|
|
|
|
var jsonSpread = function(input,options){
|
|
return jsonSpread.spread(input,options);
|
|
}
|
|
|
|
jsonSpread.spread = function(input,options){
|
|
var opts = {};
|
|
assign(opts,defaultOptions,options);
|
|
return spread(input,opts);
|
|
}
|
|
module.exports = jsonSpread;
|
|
|
|
},{"./spread.js":9,"object-assign":7}],9:[function(require,module,exports){
|
|
/*
|
|
*The MIT License (MIT)
|
|
*Copyright (c) 2016 Xun Chen
|
|
*
|
|
*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.
|
|
*/
|
|
var flat = require('flat');
|
|
var assign = require('object-assign');
|
|
var forEach = require('foreach');
|
|
var isObject = require('is-plain-object');
|
|
var flatmap = require('flatmap');
|
|
var util = require('./util.js');
|
|
|
|
module.exports = function(rootInput,options){
|
|
function spread(input){
|
|
//recursive spread function
|
|
//detect types of input
|
|
if(isObject(input)){
|
|
return spreadHelper(input);
|
|
}
|
|
else if(Array.isArray(input)){
|
|
return flatmap(input,function(item){
|
|
return spreadHelper(item);
|
|
})
|
|
}
|
|
else{
|
|
throw new TypeError('json-spread input needs to be either a plain object or an array. You inputted a typeof' + typeof input);
|
|
}
|
|
function spreadHelper(spreadInput){
|
|
spreadInput = flat(spreadInput,{safe:true,delimiter:options.delimiter});
|
|
var output = [];
|
|
var containsArray = false;
|
|
//create a default model objects with non array properties
|
|
var model = {};
|
|
forEach(spreadInput,function(value,key){
|
|
if(!Array.isArray(value)){
|
|
model[key] = value;
|
|
}
|
|
})
|
|
//iterate through each property again
|
|
forEach(spreadInput,function(value,key){
|
|
if(Array.isArray(value)){//if it is array, we test if it contains nested array or not
|
|
if(value.length === 0){ //if empty array
|
|
if(options.removeEmptyArray){
|
|
delete spreadInput[key];
|
|
}
|
|
else{
|
|
spreadInput[key] = options.emptyValue;
|
|
}
|
|
return;
|
|
}
|
|
else{
|
|
containsArray = true;
|
|
var propertyArray = [];
|
|
if(util.containsNestedArray(value)){
|
|
//if it contains nested array, we recurse down the tree
|
|
forEach(value,function(propValue,propKey){
|
|
var innerArray = spread(propValue); //this returns us an array of flattened objects
|
|
forEach(innerArray,function(item,i){
|
|
propertyArray.push(item);
|
|
});
|
|
})
|
|
}
|
|
else{ //if it does not contain nested array, we use the array as it is.
|
|
propertyArray = value;
|
|
}
|
|
//concat the model object with the object inside the property array and produce a new dataset, this new data set will exist in output;
|
|
forEach(propertyArray,function(propValue,index){
|
|
var propObj = {};
|
|
propObj[key] = propValue;
|
|
output.push(assign({},model,propObj));
|
|
})
|
|
}
|
|
}
|
|
})
|
|
|
|
if(containsArray){
|
|
output = spread(output);
|
|
return output;
|
|
}
|
|
else{
|
|
return [spreadInput];
|
|
}
|
|
}
|
|
}
|
|
return spread(rootInput);
|
|
}
|
|
|
|
},{"./util.js":10,"flat":1,"flatmap":2,"foreach":3,"is-plain-object":5,"object-assign":7}],10:[function(require,module,exports){
|
|
var flat = require('flat');
|
|
var forEach = require('foreach');
|
|
|
|
module.exports = (function(){
|
|
function containsNestedArray(value){
|
|
value = flat(value,{safe:true});
|
|
var test = false;
|
|
forEach(value,function(v,key){
|
|
if(Array.isArray(v)){
|
|
test = true;
|
|
}
|
|
})
|
|
return test;
|
|
}
|
|
function getNode(rootNode,path){
|
|
var _path = JSON.parse(JSON.stringify(path));
|
|
var _rootNode = JSON.parse(JSON.stringify(rootNode));
|
|
return recurse(_rootNode,_path);
|
|
function recurse(rootNode,path){
|
|
if (_path.length === 0){
|
|
return rootNode;
|
|
}
|
|
else{
|
|
rootNode = rootNode[path.shift()];
|
|
return getNode(rootNode,path);
|
|
}
|
|
}
|
|
}
|
|
function convertToArray(val){
|
|
return Array.isArray(val)? val : [val];
|
|
}
|
|
return {
|
|
containsNestedArray: containsNestedArray,
|
|
getNode : getNode,
|
|
convertToArray: convertToArray
|
|
}
|
|
})();
|
|
|
|
},{"flat":1,"foreach":3}]},{},[8])(8)
|
|
}); |