Initial commit
This commit is contained in:
1591
node_modules/stringifier/build/stringifier.js
generated
vendored
Normal file
1591
node_modules/stringifier/build/stringifier.js
generated
vendored
Normal file
File diff suppressed because it is too large
Load Diff
112
node_modules/stringifier/index.js
generated
vendored
Normal file
112
node_modules/stringifier/index.js
generated
vendored
Normal file
@@ -0,0 +1,112 @@
|
||||
/**
|
||||
* stringifier
|
||||
*
|
||||
* https://github.com/twada/stringifier
|
||||
*
|
||||
* Copyright (c) 2014-2018 Takuto Wada
|
||||
* Licensed under the MIT license.
|
||||
* https://twada.mit-license.org/2014-2018
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
var traverse = require('traverse');
|
||||
var typeName = require('type-name');
|
||||
var assign = require('core-js/library/fn/object/assign');
|
||||
var endsWith = require('core-js/library/fn/string/ends-with');
|
||||
var s = require('./strategies');
|
||||
|
||||
function defaultHandlers () {
|
||||
return {
|
||||
'null': s.always('null'),
|
||||
'undefined': s.always('undefined'),
|
||||
'function': s.prune(),
|
||||
'string': s.json(),
|
||||
'boolean': s.json(),
|
||||
'number': s.number(),
|
||||
'symbol': s.toStr(),
|
||||
'RegExp': s.toStr(),
|
||||
'String': s.newLike(),
|
||||
'Boolean': s.newLike(),
|
||||
'Number': s.newLike(),
|
||||
'Date': s.newLike(),
|
||||
'Array': s.array(),
|
||||
'Object': s.object(),
|
||||
'Error': s.object(null, ['message', 'code']),
|
||||
'@default': s.object()
|
||||
};
|
||||
}
|
||||
|
||||
function defaultOptions () {
|
||||
return {
|
||||
maxDepth: null,
|
||||
indent: null,
|
||||
anonymous: '@Anonymous',
|
||||
circular: '#@Circular#',
|
||||
snip: '..(snip)',
|
||||
lineSeparator: '\n',
|
||||
typeFun: typeName
|
||||
};
|
||||
}
|
||||
|
||||
function createStringifier (customOptions) {
|
||||
var options = assign({}, defaultOptions(), customOptions);
|
||||
var handlers = assign({}, defaultHandlers(), options.handlers);
|
||||
return function stringifyAny (push, x) {
|
||||
var context = this;
|
||||
var handler = handlerFor(context.node, options, handlers);
|
||||
var currentPath = '/' + context.path.join('/');
|
||||
var customization = handlers[currentPath];
|
||||
var acc = {
|
||||
context: context,
|
||||
options: options,
|
||||
handlers: handlers,
|
||||
push: push
|
||||
};
|
||||
if (typeName(customization) === 'function') {
|
||||
handler = customization;
|
||||
} else if (typeName(customization) === 'number') {
|
||||
handler = s.flow.compose(s.filters.truncate(customization),handler);
|
||||
} else if (context.parent && typeName(context.parent.node) === 'Array' && !(context.key in context.parent.node)) {
|
||||
// sparse arrays
|
||||
handler = s.always('');
|
||||
}
|
||||
handler(acc, x);
|
||||
return push;
|
||||
};
|
||||
}
|
||||
|
||||
function handlerFor (val, options, handlers) {
|
||||
var tname = options.typeFun(val);
|
||||
if (typeName(handlers[tname]) === 'function') {
|
||||
return handlers[tname];
|
||||
}
|
||||
if (endsWith(tname, 'Error')) {
|
||||
return handlers['Error'];
|
||||
}
|
||||
return handlers['@default'];
|
||||
}
|
||||
|
||||
function walk (val, reducer) {
|
||||
var buffer = [];
|
||||
var push = function (str) {
|
||||
buffer.push(str);
|
||||
};
|
||||
traverse(val).reduce(reducer, push);
|
||||
return buffer.join('');
|
||||
}
|
||||
|
||||
function stringify (val, options) {
|
||||
return walk(val, createStringifier(options));
|
||||
}
|
||||
|
||||
function stringifier (options) {
|
||||
return function (val) {
|
||||
return walk(val, createStringifier(options));
|
||||
};
|
||||
}
|
||||
|
||||
stringifier.stringify = stringify;
|
||||
stringifier.strategies = s;
|
||||
stringifier.defaultOptions = defaultOptions;
|
||||
stringifier.defaultHandlers = defaultHandlers;
|
||||
module.exports = stringifier;
|
||||
44
node_modules/stringifier/package.json
generated
vendored
Normal file
44
node_modules/stringifier/package.json
generated
vendored
Normal file
@@ -0,0 +1,44 @@
|
||||
{
|
||||
"name": "stringifier",
|
||||
"description": "Yet another stringify function",
|
||||
"version": "1.4.0",
|
||||
"author": {
|
||||
"name": "Takuto Wada",
|
||||
"email": "takuto.wada@gmail.com",
|
||||
"url": "https://github.com/twada"
|
||||
},
|
||||
"dependencies": {
|
||||
"core-js": "^2.0.0",
|
||||
"traverse": "^0.6.6",
|
||||
"type-name": "^2.0.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"browserify": "^13.0.0",
|
||||
"del": "^2.0.2",
|
||||
"gulp": "^3.9.0",
|
||||
"gulp-derequire": "^2.1.0",
|
||||
"gulp-dereserve": "^0.2.1",
|
||||
"gulp-mocha": "^2.1.3",
|
||||
"gulp-mocha-phantomjs": "^0.11.0",
|
||||
"gulp-util": "^3.0.6",
|
||||
"gulp-webserver": "^0.9.1",
|
||||
"licensify": "^3.1.0",
|
||||
"mocha": "^2.3.3",
|
||||
"vinyl-source-stream": "^1.1.0"
|
||||
},
|
||||
"files": [
|
||||
"CHANGELOG.md",
|
||||
"README.md",
|
||||
"index.js",
|
||||
"strategies.js",
|
||||
"build/stringifier.js",
|
||||
"package.json"
|
||||
],
|
||||
"homepage": "https://github.com/twada/stringifier",
|
||||
"license": "MIT",
|
||||
"main": "index.js",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git://github.com/twada/stringifier.git"
|
||||
}
|
||||
}
|
||||
413
node_modules/stringifier/strategies.js
generated
vendored
Normal file
413
node_modules/stringifier/strategies.js
generated
vendored
Normal file
@@ -0,0 +1,413 @@
|
||||
'use strict';
|
||||
|
||||
var typeName = require('type-name');
|
||||
var forEach = require('core-js/library/fn/array/for-each');
|
||||
var arrayFilter = require('core-js/library/fn/array/filter');
|
||||
var reduceRight = require('core-js/library/fn/array/reduce-right');
|
||||
var indexOf = require('core-js/library/fn/array/index-of');
|
||||
var slice = Array.prototype.slice;
|
||||
var END = {};
|
||||
var ITERATE = {};
|
||||
|
||||
// arguments should end with end or iterate
|
||||
function compose () {
|
||||
var filters = slice.apply(arguments);
|
||||
return reduceRight(filters, function(right, left) {
|
||||
return left(right);
|
||||
});
|
||||
}
|
||||
|
||||
// skip children
|
||||
function end () {
|
||||
return function (acc, x) {
|
||||
acc.context.keys = [];
|
||||
return END;
|
||||
};
|
||||
}
|
||||
|
||||
// iterate children
|
||||
function iterate () {
|
||||
return function (acc, x) {
|
||||
return ITERATE;
|
||||
};
|
||||
}
|
||||
|
||||
function filter (predicate) {
|
||||
return function (next) {
|
||||
return function (acc, x) {
|
||||
var toBeIterated;
|
||||
var isIteratingArray = (typeName(x) === 'Array');
|
||||
if (typeName(predicate) === 'function') {
|
||||
toBeIterated = [];
|
||||
forEach(acc.context.keys, function (key) {
|
||||
var indexOrKey = isIteratingArray ? parseInt(key, 10) : key;
|
||||
var kvp = {
|
||||
key: indexOrKey,
|
||||
value: x[key]
|
||||
};
|
||||
var decision = predicate(kvp);
|
||||
if (decision) {
|
||||
toBeIterated.push(key);
|
||||
}
|
||||
if (typeName(decision) === 'number') {
|
||||
truncateByKey(decision, key, acc);
|
||||
}
|
||||
if (typeName(decision) === 'function') {
|
||||
customizeStrategyForKey(decision, key, acc);
|
||||
}
|
||||
});
|
||||
acc.context.keys = toBeIterated;
|
||||
}
|
||||
return next(acc, x);
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
function customizeStrategyForKey (strategy, key, acc) {
|
||||
acc.handlers[currentPath(key, acc)] = strategy;
|
||||
}
|
||||
|
||||
function truncateByKey (size, key, acc) {
|
||||
acc.handlers[currentPath(key, acc)] = size;
|
||||
}
|
||||
|
||||
function currentPath (key, acc) {
|
||||
var pathToCurrentNode = [''].concat(acc.context.path);
|
||||
if (typeName(key) !== 'undefined') {
|
||||
pathToCurrentNode.push(key);
|
||||
}
|
||||
return pathToCurrentNode.join('/');
|
||||
}
|
||||
|
||||
function allowedKeys (orderedWhiteList) {
|
||||
return function (next) {
|
||||
return function (acc, x) {
|
||||
var isIteratingArray = (typeName(x) === 'Array');
|
||||
if (!isIteratingArray && typeName(orderedWhiteList) === 'Array') {
|
||||
acc.context.keys = arrayFilter(orderedWhiteList, function (propKey) {
|
||||
return x.hasOwnProperty(propKey);
|
||||
});
|
||||
}
|
||||
return next(acc, x);
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
function safeKeys () {
|
||||
return function (next) {
|
||||
return function (acc, x) {
|
||||
if (typeName(x) !== 'Array') {
|
||||
acc.context.keys = arrayFilter(acc.context.keys, function (propKey) {
|
||||
// Error handling for unsafe property access.
|
||||
// For example, on PhantomJS,
|
||||
// accessing HTMLInputElement.selectionEnd causes TypeError
|
||||
try {
|
||||
var val = x[propKey];
|
||||
return true;
|
||||
} catch (e) {
|
||||
// skip unsafe key
|
||||
return false;
|
||||
}
|
||||
});
|
||||
}
|
||||
return next(acc, x);
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
function arrayIndicesToKeys () {
|
||||
return function (next) {
|
||||
return function (acc, x) {
|
||||
if (typeName(x) === 'Array' && 0 < x.length) {
|
||||
var indices = Array(x.length);
|
||||
for(var i = 0; i < x.length; i += 1) {
|
||||
indices[i] = String(i); // traverse uses strings as keys
|
||||
}
|
||||
acc.context.keys = indices;
|
||||
}
|
||||
return next(acc, x);
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
function when (guard, then) {
|
||||
return function (next) {
|
||||
return function (acc, x) {
|
||||
var kvp = {
|
||||
key: acc.context.key,
|
||||
value: x
|
||||
};
|
||||
if (guard(kvp, acc)) {
|
||||
return then(acc, x);
|
||||
}
|
||||
return next(acc, x);
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
function truncate (size) {
|
||||
return function (next) {
|
||||
return function (acc, x) {
|
||||
var orig = acc.push;
|
||||
var ret;
|
||||
acc.push = function (str) {
|
||||
var savings = str.length - size;
|
||||
var truncated;
|
||||
if (savings <= size) {
|
||||
orig.call(acc, str);
|
||||
} else {
|
||||
truncated = str.substring(0, size);
|
||||
orig.call(acc, truncated + acc.options.snip);
|
||||
}
|
||||
};
|
||||
ret = next(acc, x);
|
||||
acc.push = orig;
|
||||
return ret;
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
function constructorName () {
|
||||
return function (next) {
|
||||
return function (acc, x) {
|
||||
var name = acc.options.typeFun(x);
|
||||
if (name === '') {
|
||||
name = acc.options.anonymous;
|
||||
}
|
||||
acc.push(name);
|
||||
return next(acc, x);
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
function always (str) {
|
||||
return function (next) {
|
||||
return function (acc, x) {
|
||||
acc.push(str);
|
||||
return next(acc, x);
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
function optionValue (key) {
|
||||
return function (next) {
|
||||
return function (acc, x) {
|
||||
acc.push(acc.options[key]);
|
||||
return next(acc, x);
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
function json (replacer) {
|
||||
return function (next) {
|
||||
return function (acc, x) {
|
||||
acc.push(JSON.stringify(x, replacer));
|
||||
return next(acc, x);
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
function toStr () {
|
||||
return function (next) {
|
||||
return function (acc, x) {
|
||||
acc.push(x.toString());
|
||||
return next(acc, x);
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
function decorateArray () {
|
||||
return function (next) {
|
||||
return function (acc, x) {
|
||||
acc.context.before(function (node) {
|
||||
acc.push('[');
|
||||
});
|
||||
acc.context.after(function (node) {
|
||||
afterAllChildren(this, acc.push, acc.options);
|
||||
acc.push(']');
|
||||
});
|
||||
acc.context.pre(function (val, key) {
|
||||
beforeEachChild(this, acc.push, acc.options);
|
||||
});
|
||||
acc.context.post(function (childContext) {
|
||||
afterEachChild(childContext, acc.push);
|
||||
});
|
||||
return next(acc, x);
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
function decorateObject () {
|
||||
return function (next) {
|
||||
return function (acc, x) {
|
||||
acc.context.before(function (node) {
|
||||
acc.push('{');
|
||||
});
|
||||
acc.context.after(function (node) {
|
||||
afterAllChildren(this, acc.push, acc.options);
|
||||
acc.push('}');
|
||||
});
|
||||
acc.context.pre(function (val, key) {
|
||||
beforeEachChild(this, acc.push, acc.options);
|
||||
acc.push(sanitizeKey(key) + (acc.options.indent ? ': ' : ':'));
|
||||
});
|
||||
acc.context.post(function (childContext) {
|
||||
afterEachChild(childContext, acc.push);
|
||||
});
|
||||
return next(acc, x);
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
function sanitizeKey (key) {
|
||||
return /^[A-Za-z_]+$/.test(key) ? key : JSON.stringify(key);
|
||||
}
|
||||
|
||||
function afterAllChildren (context, push, options) {
|
||||
if (options.indent && 0 < context.keys.length) {
|
||||
push(options.lineSeparator);
|
||||
for(var i = 0; i < context.level; i += 1) { // indent level - 1
|
||||
push(options.indent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function beforeEachChild (context, push, options) {
|
||||
if (options.indent) {
|
||||
push(options.lineSeparator);
|
||||
for(var i = 0; i <= context.level; i += 1) {
|
||||
push(options.indent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function afterEachChild (childContext, push) {
|
||||
if (!childContext.isLast) {
|
||||
push(',');
|
||||
}
|
||||
}
|
||||
|
||||
function nan (kvp, acc) {
|
||||
return kvp.value !== kvp.value;
|
||||
}
|
||||
|
||||
function positiveInfinity (kvp, acc) {
|
||||
return !isFinite(kvp.value) && kvp.value === Infinity;
|
||||
}
|
||||
|
||||
function negativeInfinity (kvp, acc) {
|
||||
return !isFinite(kvp.value) && kvp.value !== Infinity;
|
||||
}
|
||||
|
||||
function circular (kvp, acc) {
|
||||
return acc.context.circular;
|
||||
}
|
||||
|
||||
function maxDepth (kvp, acc) {
|
||||
return (acc.options.maxDepth && acc.options.maxDepth <= acc.context.level);
|
||||
}
|
||||
|
||||
var prune = compose(
|
||||
always('#'),
|
||||
constructorName(),
|
||||
always('#'),
|
||||
end()
|
||||
);
|
||||
var omitNaN = when(nan, compose(
|
||||
always('NaN'),
|
||||
end()
|
||||
));
|
||||
var omitPositiveInfinity = when(positiveInfinity, compose(
|
||||
always('Infinity'),
|
||||
end()
|
||||
));
|
||||
var omitNegativeInfinity = when(negativeInfinity, compose(
|
||||
always('-Infinity'),
|
||||
end()
|
||||
));
|
||||
var omitCircular = when(circular, compose(
|
||||
optionValue('circular'),
|
||||
end()
|
||||
));
|
||||
var omitMaxDepth = when(maxDepth, prune);
|
||||
|
||||
module.exports = {
|
||||
filters: {
|
||||
always: always,
|
||||
optionValue: optionValue,
|
||||
constructorName: constructorName,
|
||||
json: json,
|
||||
toStr: toStr,
|
||||
prune: prune,
|
||||
truncate: truncate,
|
||||
decorateArray: decorateArray,
|
||||
decorateObject: decorateObject
|
||||
},
|
||||
flow: {
|
||||
compose: compose,
|
||||
when: when,
|
||||
allowedKeys: allowedKeys,
|
||||
safeKeys: safeKeys,
|
||||
arrayIndicesToKeys: arrayIndicesToKeys,
|
||||
filter: filter,
|
||||
iterate: iterate,
|
||||
end: end
|
||||
},
|
||||
symbols: {
|
||||
END: END,
|
||||
ITERATE: ITERATE
|
||||
},
|
||||
always: function (str) {
|
||||
return compose(always(str), end());
|
||||
},
|
||||
json: function () {
|
||||
return compose(json(), end());
|
||||
},
|
||||
toStr: function () {
|
||||
return compose(toStr(), end());
|
||||
},
|
||||
prune: function () {
|
||||
return prune;
|
||||
},
|
||||
number: function () {
|
||||
return compose(
|
||||
omitNaN,
|
||||
omitPositiveInfinity,
|
||||
omitNegativeInfinity,
|
||||
json(),
|
||||
end()
|
||||
);
|
||||
},
|
||||
newLike: function () {
|
||||
return compose(
|
||||
always('new '),
|
||||
constructorName(),
|
||||
always('('),
|
||||
json(),
|
||||
always(')'),
|
||||
end()
|
||||
);
|
||||
},
|
||||
array: function (predicate) {
|
||||
return compose(
|
||||
omitCircular,
|
||||
omitMaxDepth,
|
||||
decorateArray(),
|
||||
arrayIndicesToKeys(),
|
||||
filter(predicate),
|
||||
iterate()
|
||||
);
|
||||
},
|
||||
object: function (predicate, orderedWhiteList) {
|
||||
return compose(
|
||||
omitCircular,
|
||||
omitMaxDepth,
|
||||
constructorName(),
|
||||
decorateObject(),
|
||||
allowedKeys(orderedWhiteList),
|
||||
safeKeys(),
|
||||
filter(predicate),
|
||||
iterate()
|
||||
);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user