New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

chimpanzee

Package Overview
Dependencies
Maintainers
1
Versions
79
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

chimpanzee - npm Package Compare versions

Comparing version 0.0.2 to 0.0.3

dist/deep.js

25

dist/any.js

@@ -12,2 +12,4 @@ "use strict";

var _lazilyAsync = require("lazily-async");
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

@@ -17,13 +19,18 @@

return function () {
var _ref = _asyncToGenerator(function* (obj, context, key, parentObj, parentContext) {
for (const gen of list) {
const result = yield (0, _chimpanzee.match)(gen(obj, context, key, parentObj, parentContext));
if (result && result.type === "return") {
return result;
}
}
return (0, _wrap.skip)("None of the candidates matched.");
var _ref = _asyncToGenerator(function* (obj, context, key) {
return list.length ? (yield _lazilyAsync.Seq.of(list).map(function () {
var _ref2 = _asyncToGenerator(function* (gen) {
const result = yield (0, _chimpanzee.match)(gen(obj, context, key));
return result.type === "return" ? result : undefined;
});
return function (_x4) {
return _ref2.apply(this, arguments);
};
}()).filter(function (r) {
return r;
}).first()) || (0, _wrap.skip)("None of the items matched.") : (0, _wrap.ret)({});
});
return function (_x, _x2, _x3, _x4, _x5) {
return function (_x, _x2, _x3) {
return _ref.apply(this, arguments);

@@ -30,0 +37,0 @@ };

@@ -0,8 +1,125 @@

// import { wrap, unwrap, skip } from "./wrap";
// import { match } from "./chimpanzee";
//
// export function repeating(gen, { min, max }) {
// return wrap(gen, { type: "repeating", min, max })
// }
//
// export function unordered(gen) {
// return wrap(gen, { type: "unordered" })
// }
//
// export function optional(gen) {
// return wrap(gen, { type: "optional" })
// }
//
// function _unwrap(something) {
// if (isWrapped(something)) {
// return { type: something.type, item: unwrap(something) }
// } else {
// return { item: something }
// }
// }
//
// export function array(list) {
// return async function(obj, context, key) {
//
// function getMatcher() {
// const { type, item } = _unwrap(_gen);
// if (type === "unordered") {
// const { type: type2, item: item2 } = _unwrap(item);
// if (type2 === "repeating") {
// const match = findUnorderedRepeating(item2, type2.min, type2.max, needle)
// }
// else if (type2) {
// throw new Error(`Unordered array item can only be repeating or non-repeating. Got ${type2}.`)
// }
// else {
// const match = findUnordered();
// }
// } else if (type === "repeating") {
// const { type: type2, item: item2 } = _unwrap(item);
// if (type2 === "optional") {
// const match = findrepeatingOptional();
// }
// else if (type2) {
// throw new Error(`Repeating array item can only be optional or mandatory. Got ${type2}.`)
// }
// else {
// const match = findRepeating();
// }
// }
// else if (type === "optional") {
// const match = findOptional();
// }
// else {
// const match = findItem();
// }
// }
//
// function async findUnordered(gen, needle) {
// for (const item of obj) {
// const result = await match(gen(item, context, key));
// if (result && result.type === "return") {
// return { result: result.value, needle }
// }
// }
// return { skip: "Unordered item not found" }
// }
//
// function async findUnorderedRepeating(gen, min, max, needle) {
// const results = [];
// for (const item of obj) {
// const result = await match(gen(item, context, key));
// if (result && result.type === "return") {
// results.push(result.value)
// }
// }
//
//
//
// return ((!min || results.length >= min) && (!max || results.length <= max)) ?
// { result: results, needle } : { skip: "Unordered item not found" };
// }
//
// function async findRepeating(gen, min, max, needle) {
// for (const i = needle; i++; i < obj.length) {
// const item = obj[i];
// const result = await match(gen(item, context, key));
// if (result && result.type === "return") {
// results.push(result.value)
// } else {
//
// }
// }
// }
//
// function async findrepeatingOptional() {
//
// }
//
// function async findOptional() {
//
// }
//
// function async findItem() {
//
// }
//
// if (Array.isArray(obj)) {
// const results = [];
//
// let arrayNeedle = 0;
// for (const _gen of list) {
// const { matcher } = getMatcher(_gen);
//
// }
//
// }
// else {
// return skip("Not an array.")
// }
// }
// }
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.array = array;
function array(schema) {}
//# sourceMappingURL=array.js.map

@@ -14,22 +14,23 @@ "use strict";

var _chimpanzee = require("./chimpanzee");
var _wrap = require("./wrap");
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }
function capture(name, schema, options = {}) {
function capture(name, gen) {
return captureIf(function (obj) {
return typeof obj !== "undefined";
}, name, gen);
}
function captureIf(predicate, name, gen) {
return function () {
var _ref = _asyncToGenerator(function* (obj, context, key, parentObj, parentContext) {
const _options = _extends({
predicate: function predicate(obj) {
return typeof obj !== "undefined";
}
}, options, {
builders: (options.builders || []).concat({
get: function get(obj) {
return { [name || key]: obj };
}
})
});
return (0, _traverse.traverse)(schema || {}, _options)(obj, context, key, parentObj, parentContext);
var _ref = _asyncToGenerator(function* (obj, context, key) {
const inner = gen ? yield (0, _chimpanzee.match)(gen(obj, context, key)) : {};
const captured = predicate(obj) ? { [name || key]: obj } : undefined;
return gen ? inner.type === "return" ? (0, _wrap.ret)(_extends({}, captured, inner.value)) : inner : captured ? (0, _wrap.ret)(captured) : (0, _wrap.skip)("Predicate returned false.");
});
return function (_x, _x2, _x3, _x4, _x5) {
return function (_x, _x2, _x3) {
return _ref.apply(this, arguments);

@@ -39,6 +40,2 @@ };

}
function captureIf(predicate, name, schema) {
return capture(name, schema, { predicate });
}
//# sourceMappingURL=capture.js.map

@@ -10,7 +10,3 @@ "use strict";

const result = yield traverseResult;
if (typeof result === "function") {
return yield match(result());
} else {
return result;
}
return typeof result === "function" ? yield match(result()) : result;
});

@@ -56,2 +52,29 @@

var _map = require("./map");
Object.defineProperty(exports, "map", {
enumerable: true,
get: function get() {
return _map.map;
}
});
var _optional = require("./optional");
Object.defineProperty(exports, "optional", {
enumerable: true,
get: function get() {
return _optional.optional;
}
});
var _deep = require("./deep");
Object.defineProperty(exports, "deep", {
enumerable: true,
get: function get() {
return _deep.deep;
}
});
var _empty = require("./empty");

@@ -58,0 +81,0 @@

@@ -14,7 +14,7 @@ "use strict";

return function () {
var _ref = _asyncToGenerator(function* (obj, context, key, parentObj, parentContext) {
return obj === undefined ? (0, _wrap.ret)({}) : (0, _wrap.skip)("Not empty.");
var _ref = _asyncToGenerator(function* (obj, context, key) {
return obj === undefined ? (0, _wrap.ret)(context.state) : (0, _wrap.skip)("Not empty.");
});
return function (_x, _x2, _x3, _x4, _x5) {
return function (_x, _x2, _x3) {
return _ref.apply(this, arguments);

@@ -21,0 +21,0 @@ };

@@ -13,139 +13,172 @@ "use strict";

var _lazilyAsync = require("lazily-async");
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }
function traverse(schema, options = {}) {
if (!options.builders) {
options.builders = [{ get: function get(obj, { state }) {
return state;
} }];
}
let ctr = 0;
function traverse(schema, options = {}, newContext = true) {
options.ctr = ctr++;
options.builders = options.builders || [{ get: function get(obj, { state }) {
return state;
} }];
return function () {
var _ref = _asyncToGenerator(function* (obj, context = { state: {} }, key, parentObj, parentContext) {
var _ref = _asyncToGenerator(function* (_obj, _context, key) {
let getTask = function () {
var _ref2 = _asyncToGenerator(function* (builder) {
return function () {
var _ref3 = _asyncToGenerator(function* () {
const readyToRun = !builder.precondition || (yield builder.precondition(obj, context, key));
return readyToRun ? yield _asyncToGenerator(function* () {
const predicates = (!builder.predicates ? [] : builder.predicates.map(function (p) {
return { fn: p.predicate, invalid: function invalid() {
return (0, _wrap.skip)(p.message || `Predicate returned false.`);
} };
})).concat(!builder.asserts ? [] : builder.asserts.map(function (a) {
return { fn: a.predicate, invalid: function invalid() {
return (0, _wrap.error)(a.error);
} };
}));
/*
Tasks must run only after childTasks are complete.
*/
let run = function () {
var _ref3 = _asyncToGenerator(function* (childTasks, tasks) {
const runnables = childTasks.length ? childTasks : tasks;
return (yield _lazilyAsync.Seq.of(predicates).map(function () {
var _ref5 = _asyncToGenerator(function* (predicate) {
return !(yield predicate.fn(obj, context, key)) ? predicate.invalid() : undefined;
});
let unfinished = [],
finished = [];
for (const gen of runnables) {
const val = yield gen;
if (typeof val === "function") {
unfinished.push(val());
} else {
finished.push(val);
}
}
return function (_x5) {
return _ref5.apply(this, arguments);
};
}()).first(function (x) {
return x;
})) || (0, _wrap.ret)((yield builder.get(obj, context, key)));
})() : fn;
});
for (const item of finished) {
if (item.type === "skip" || item.type === "error") {
return item;
} else if (item.type === "return") {
context.state = _extends({}, context.state, item.value);
} else {
throw new Error(`Unknown result ${ item }.`);
function fn() {
return _ref3.apply(this, arguments);
}
}
if (childTasks.length || unfinished.length) {
return _asyncToGenerator(function* () {
return childTasks.length ? run(unfinished, tasks) : run([], unfinished);
return fn;
}();
});
return function getTask(_x4) {
return _ref2.apply(this, arguments);
};
}();
let getObjectTasks = function () {
var _ref6 = _asyncToGenerator(function* () {
return typeof obj !== "undefined" ? yield _lazilyAsync.Seq.of(Object.keys(schema)).map(function () {
var _ref7 = _asyncToGenerator(function* (key) {
const lhs = options.modifier ? yield options.modifier(obj, key) : obj[key];
const rhs = schema[key];
return ["string", "number", "boolean"].includes(typeof rhs) ? lhs !== rhs ? (0, _wrap.skip)(`Expected ${ rhs } but got ${ lhs }.`) : undefined : typeof rhs === "object" ? traverse(rhs, options, false)(lhs, context, key) : typeof rhs === "function" ? rhs(lhs, context, key) : (0, _wrap.error)(`Cannot traverse ${ typeof rhs }.`);
});
} else {
return (0, _wrap.ret)(context.state);
}
return function (_x6) {
return _ref7.apply(this, arguments);
};
}()).filter(function (x) {
return x;
}).reduce(function (acc, x) {
return !["skip", "error"].includes(x.type) ? acc.concat(x) : [x];
}, [], function (acc, x) {
return x && ["skip", "error"].includes(x.type);
}) : [(0, _wrap.skip)(`Cannot traverse undefined.`)];
});
return function run(_x2, _x3) {
return _ref3.apply(this, arguments);
return function getObjectTasks() {
return _ref6.apply(this, arguments);
};
}();
obj = options.objectModifier ? yield options.objectModifier(obj) : obj;
let getArrayTasks = function () {
var _ref8 = _asyncToGenerator(function* () {
return Array.isArray(obj) ? schema.length !== obj.length ? (0, _wrap.skip)(`Expected array of length ${ schema.length } but got ${ obj.length }.`) : yield _lazilyAsync.Seq.of(schema).map(function (rhs, i) {
const lhs = obj[i];
return traverse(rhs, options, false)(lhs, context, `${ key }_${ i }`);
}) : [(0, _wrap.skip)(`Schema is an array but property is a non-array.`)];
});
if (options.predicate && !(yield options.predicate(obj))) {
return (0, _wrap.skip)(`Predicate returned false.`);
}
return function getArrayTasks() {
return _ref8.apply(this, arguments);
};
}();
function getTask(builder) {
return function () {
var _ref2 = _asyncToGenerator(function* () {
if (!builder.precondition || (yield builder.precondition(obj, context, key, parentObj, parentContext))) {
const predicates = (!builder.predicates ? [] : builder.predicates.map(function (p) {
return { fn: p.predicate, invalid: function invalid() {
return (0, _wrap.skip)(`Predicate returned false.`);
} };
})).concat(!builder.asserts ? [] : builder.asserts.map(function (a) {
return { fn: a.predicate, invalid: function invalid() {
return (0, _wrap.error)(a.error);
} };
}));
let getFunctionTasks = function () {
var _ref9 = _asyncToGenerator(function* () {
return [schema(obj, context, key)];
});
for (const predicate of predicates) {
if (!(yield predicate.fn(obj, context, key, parentObj, parentContext))) {
return predicate.invalid();
}
}
return function getFunctionTasks() {
return _ref9.apply(this, arguments);
};
}();
return (0, _wrap.ret)((yield builder.get(obj, context, key, parentObj, parentContext)));
} else {
return fn;
}
/*
Tasks must run only after childTasks are complete.
*/
let run = function () {
var _ref10 = _asyncToGenerator(function* (childTasks, tasks) {
const isRunningChildTasks = childTasks.length;
const runnables = isRunningChildTasks ? childTasks : tasks;
const { finished, unfinished } = yield _lazilyAsync.Seq.of(runnables).map(function () {
var _ref11 = _asyncToGenerator(function* (gen) {
return yield gen;
});
return function (_x9) {
return _ref11.apply(this, arguments);
};
}()).reduce(function (acc, item) {
return typeof item === "function" ? { finished: acc.finished, unfinished: acc.unfinished.concat(item()) } : { finished: acc.finished.concat(item), unfinished: acc.unfinished };
}, { finished: [], unfinished: [] });
//Mutation. Global state for traversal.
const { state, nonResult } = yield _lazilyAsync.Seq.of(finished).reduce(function (acc, item) {
return item.type === "return" ? isRunningChildTasks ? Object.assign(acc, { state: _extends({}, acc.state, item.value) }) : Object.assign(acc, { state: item.value }) : { nonResult: item };
}, context, function (acc, item) {
return item.type !== "return";
});
function fn() {
return _ref2.apply(this, arguments);
}
return nonResult ? nonResult : childTasks.length || unfinished.length ? isRunningChildTasks ? run(unfinished, tasks) : run([], unfinished) : (0, _wrap.ret)(context.state);
});
return fn;
}();
}
return function run(_x7, _x8) {
return _ref10.apply(this, arguments);
};
}();
const tasks = options.builders.map(function (builder) {
return getTask(builder);
});
const context = newContext ? { state: {}, parent: _context } : _context || { state: {} };
let childTasks = [];
const obj = options.objectModifier ? yield options.objectModifier(_obj) : _obj;
if (typeof schema === "object") {
for (const key in schema) {
const lhs = options.modifier ? yield options.modifier(obj, key) : obj[key];
const rhs = schema[key];
const mustRun = !options.predicate || (yield options.predicate(obj));
if (["string", "number", "boolean"].includes(typeof rhs)) {
if (lhs !== rhs) {
return (0, _wrap.skip)(`Expected ${ rhs } but got ${ lhs }.`);
}
} else if (typeof rhs === "object") {
childTasks.push(traverse(rhs, options)(lhs, context, key, parentObj, parentContext));
} else if (typeof rhs === "function") {
childTasks.push(rhs(lhs, undefined, key, obj, context));
}
}
} else if (Array.isArray(schema)) {
if (!Array.isArray(obj)) {
return (0, _wrap.skip)(`Expected array but got ${ typeof obj }.`);
}
return !mustRun ? (0, _wrap.skip)(`Predicate returned false.`) : _asyncToGenerator(function* () {
const tasks = yield _lazilyAsync.Seq.of(options.builders).map(function () {
var _ref13 = _asyncToGenerator(function* (builder) {
return yield getTask(builder);
});
if (schema.length !== obj.length) {
return (0, _wrap.skip)(`Expected array of length ${ schema.length } but got ${ obj.length }.`);
}
return function (_x10) {
return _ref13.apply(this, arguments);
};
}()).toArray();
for (let i = 0; i < schema.length; i++) {
const lhs = obj[i];
const rhs = schema[i];
childTasks.push(traverse(rhs, options)(lhs, context, i, parentObj, parentContext));
}
}
const childTasks = typeof schema === "object" ? yield getObjectTasks() : Array.isArray(schema) ? yield getArrayTasks() : typeof schema === "function" ? yield getFunctionTasks() : [];
return _asyncToGenerator(function* () {
return run(childTasks, tasks);
});
return _asyncToGenerator(function* () {
return run(childTasks, tasks);
});
})();
});
return function (_x) {
return function (_x, _x2, _x3) {
return _ref.apply(this, arguments);

@@ -152,0 +185,0 @@ };

@@ -6,5 +6,24 @@ "use strict";

});
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
exports.wrap = wrap;
exports.unwrap = unwrap;
exports.isWrapped = isWrapped;
exports.error = error;
exports.skip = skip;
exports.ret = ret;
function wrap(item, meta) {
return _extends({ __isWrapped: true, __item: item }, meta);
}
function unwrap(wrapped) {
const { __isWrapped, __item } = wrapped;
return __isWrapped ? __item : error("This is not a wrapped object.");
}
function isWrapped(wrapped) {
return wrapped.__isWrapped;
}
function error(message) {

@@ -11,0 +30,0 @@ return { type: "error", message };

{
"name": "chimpanzee",
"version": "0.0.2",
"version": "0.0.3",
"author": "Jeswin<jeswinpk@agilehead.com>",

@@ -9,5 +9,8 @@ "scripts": {

"main": "dist/chimpanzee.js",
"dependencies": {},
"dependencies": {
"lazily-async": "0.0.5"
},
"devDependencies": {
"babel-core": "^6.22.1",
"babel-polyfill": "^6.23.0",
"babel-preset-latest": "^6.22.0",

@@ -14,0 +17,0 @@ "babel-preset-modern-browsers": "^8.0.0",

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc