UNPKG

gentyl

Version:

A Generator That You'll Love

394 lines (393 loc) 15.1 kB
var Gentyl; (function (Gentyl) { var Util; (function (Util) { function identity(x) { return x; } Util.identity = identity; function weightedChoice(weights) { var sum = weights.reduce(function (a, b) { return a + b; }, 0); var cdfArray = weights.reduce(function (coll, next, i) { var v = (coll[i - 1] || 0) + next / sum; return coll.concat([v]); }, []); var r = Math.random(); var i = 0; while (i < weights.length - 1 && r > cdfArray[i]) { i++; } return i; } Util.weightedChoice = weightedChoice; function range() { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i - 0] = arguments[_i]; } var beg, end, step; switch (args.length) { case 1: { end = args[0]; beg = 0; step = 1; break; } case 2: { end = args[1]; beg = args[0]; step = 1; break; } case 3: { end = args[2]; beg = args[0]; step = args[1]; break; } default: { end = 0; beg = 0; step = 1; break; } } var rng = []; if (beg > end && step < 0) { for (var i = beg; i > end; i += step) { rng.push(i); } } else if (beg < end && step > 0) { for (var i = beg; i < end; i += step) { rng.push(i); } } else { throw new Error("invalid range parameters"); } return rng; } Util.range = range; function translator(node, translation) { var translated; if (typeof (node) == "object" && !(node instanceof Array)) { translated = {}; for (var k in node) { var tval = translation[k]; if (typeof (tval) == "function") { translated[tval.name] = tval(node[k]); } if (typeof (tval) == "string") { translated[tval] = node[k]; } else if (tval != undefined) { translated[k] = translator(node[k], tval); } else { translated[k] = node[k]; } } return translated; } else { return node; } } Util.translator = translator; function melder(node1, node2, merge, concatArrays) { if (merge === void 0) { merge = function (a, b) { return b; }; } if (concatArrays === void 0) { concatArrays = false; } if (node1 == undefined) { return node2; } if (node2 == undefined) { return node1; } if (typeof (node1) != typeof (node2)) { var errmsg = "Expected melding nodes to be the same type \n" + "type of node1: " + typeof (node1) + "\n" + "type of node2: " + typeof (node2) + "\n"; throw TypeError(errmsg); } var melded; if (node1 instanceof Array) { return concatArrays ? node1.concat(node2) : merge(node1, node2); } else if (typeof (node1) == 'object') { melded = {}; for (var k in node1) { melded[k] = node1[k]; } for (var q in node2) { melded[q] = node2[q]; } for (var k in node1) { for (var q in node2) { if (k == q) { if (node1[k] == node2[k]) { melded[k] = node1[k]; } else { melded[k] = melder(node1[k], node2[k], merge, concatArrays); } } } } } else { melded = merge(node1, node2); } return melded; } Util.melder = melder; function deeplyEquals(node1, node2, allowIdentical) { if (allowIdentical === void 0) { allowIdentical = true; } if (typeof (node1) != typeof (node2)) { return false; } else if (node1 instanceof Object) { if (node1 === node2 && !allowIdentical) { return false; } else { for (var k in node1) { if (!(k in node2)) { return false; } } for (var q in node2) { if (!(q in node1)) { return false; } else if (!deeplyEquals(node1[q], node2[q], allowIdentical)) { return false; } } return true; } } else { return (node1 === node2); } } Util.deeplyEquals = deeplyEquals; function deeplyEqualsThrow(node1, node2, derefstack, seen, allowIdentical) { if (allowIdentical === void 0) { allowIdentical = true; } var derefstack = derefstack || []; var seen = seen || []; if (seen.indexOf(node1) || seen.indexOf(node2)) { return; } if (typeof (node1) != typeof (node2)) { throw new Error("nodes not same type, derefs: [" + derefstack + "]"); } else if (node1 instanceof Object) { if (node1 === node2 && !allowIdentical) { throw new Error("identical object not replica, derefs:[" + derefstack + "]"); } else { for (var k in node1) { if (!(k in node2)) { throw new Error("key " + k + " in object1 but not object2, derefs:[" + derefstack + "]"); } } for (var q in node2) { if (!(q in node1)) { throw new Error("key " + k + " in object2 but not object1, derefs:[" + derefstack + "]"); } else { deeplyEqualsThrow(node1[q], node2[q], derefstack.concat(q), allowIdentical); } } return true; } } else if (node1 !== node2) { throw new Error(node1 + " and " + node2 + " not equal, derefs:[" + derefstack + "]"); } } Util.deeplyEqualsThrow = deeplyEqualsThrow; function isDeepReplica(node1, node2) { deeplyEquals(node1, node2, false); } Util.isDeepReplica = isDeepReplica; function isDeepReplicaThrow(node1, node2, derefstack) { deeplyEqualsThrow(node1, node2, derefstack, null, false); } Util.isDeepReplicaThrow = isDeepReplicaThrow; function softAssoc(from, onto) { for (var k in from) { onto[k] = melder(from[k], onto[k]); } } Util.softAssoc = softAssoc; function parassoc(from, onto) { for (var k in from) { onto[k] = melder(onto[k], from[k], function (a, b) { return [a, b]; }, true); } } Util.parassoc = parassoc; function assoc(from, onto) { for (var k in from) { onto[k] = melder(onto[k], from[k]); } } Util.assoc = assoc; function deepCopy(thing) { return typeCaseSplitF(deepCopy, deepCopy)(thing); } Util.deepCopy = deepCopy; function applyMixins(derivedCtor, baseCtors) { baseCtors.forEach(function (baseCtor) { Object.getOwnPropertyNames(baseCtor.prototype).forEach(function (name) { derivedCtor.prototype[name] = baseCtor.prototype[name]; }); }); } Util.applyMixins = applyMixins; function isPrimative(thing) { return typeof (thing) !== 'object'; } Util.isPrimative = isPrimative; function isVanillaObject(thing) { return thing instanceof Object && Object.prototype == Object.getPrototypeOf(thing); } Util.isVanillaObject = isVanillaObject; function isVanillaArray(thing) { return thing instanceof Array && Array.prototype == Object.getPrototypeOf(thing); } Util.isVanillaArray = isVanillaArray; function isTree(thing, stack) { if (stack === void 0) { stack = []; } stack = stack.concat(thing); function decirc(proposed) { if ((stack.indexOf(proposed) === -1)) { return isTree(proposed, stack); } else { return false; } } return typeCaseSplitR(decirc, decirc, function () { return true; })(thing, true, function (a, b, k) { return a && b; }); } Util.isTree = isTree; function isVanillaTree(thing, stack) { if (stack === void 0) { stack = []; } function decirc(proposed) { if ((isVanillaObject(proposed) || isVanillaArray(proposed) && stack.indexOf(proposed) === -1)) { return isVanillaTree(proposed, stack.concat(proposed)); } else { return false; } } return typeCaseSplitR(decirc, decirc, isPrimative)(thing, true, function (a, b, k) { return a && b; }); } Util.isVanillaTree = isVanillaTree; function typeCaseSplitR(objectOrAllFunction, arrayFunc, primativeFunc) { var ofunc, afunc, pfunc; if (primativeFunc == undefined && arrayFunc == undefined) { ofunc = objectOrAllFunction || identity; afunc = objectOrAllFunction || identity; pfunc = objectOrAllFunction || identity; } else { ofunc = objectOrAllFunction || identity; afunc = arrayFunc || identity; pfunc = primativeFunc || identity; } return function (inThing, initial, reductor) { if (initial === void 0) { initial = null; } if (reductor === void 0) { reductor = function (a, b, k) { }; } var result = initial; if (inThing instanceof Array) { for (var i = 0; i < inThing.length; i++) { var subBundle = inThing[i]; result = reductor(result, afunc(subBundle, i), i); } } else if (isVanillaObject(inThing)) { for (var k in inThing) { var subBundle = inThing[k]; result = reductor(result, ofunc(subBundle, k), k); } } else { result = pfunc(inThing); } return result; }; } Util.typeCaseSplitR = typeCaseSplitR; function typeCaseSplitF(objectOrAllFunction, arrayFunc, primativeFunc) { var ofunc, afunc, pfunc; if (primativeFunc == undefined && arrayFunc == undefined) { ofunc = objectOrAllFunction || identity; afunc = objectOrAllFunction || identity; pfunc = objectOrAllFunction || identity; } else { ofunc = objectOrAllFunction || identity; afunc = arrayFunc || identity; pfunc = primativeFunc || identity; } return function (inThing) { var outThing; if (inThing instanceof Array) { outThing = []; outThing.length = inThing.length; for (var i = 0; i < inThing.length; i++) { var subBundle = inThing[i]; outThing[i] = afunc(subBundle, i); } } else if (isVanillaObject(inThing)) { outThing = {}; for (var k in inThing) { var subBundle = inThing[k]; outThing[k] = ofunc(subBundle, k); } } else { outThing = pfunc(inThing); } return outThing; }; } Util.typeCaseSplitF = typeCaseSplitF; function typeCaseSplitM(objectOrAllFunction, arrayFunc, primativeFunc) { var ofunc, afunc, pfunc; if (primativeFunc == undefined && arrayFunc == undefined) { ofunc = objectOrAllFunction || identity; afunc = objectOrAllFunction || identity; pfunc = objectOrAllFunction || identity; } else { ofunc = objectOrAllFunction || identity; afunc = arrayFunc || identity; pfunc = primativeFunc || identity; } return function (inThing) { if (inThing instanceof Array) { for (var i = 0; i < inThing.length; i++) { var subBundle = inThing[i]; inThing[i] = afunc(subBundle, i); } } else if (isVanillaObject) { for (var k in inThing) { var subBundle = inThing[k]; inThing[k] = ofunc(subBundle, k); } } else { pfunc(inThing); } }; } Util.typeCaseSplitM = typeCaseSplitM; })(Util = Gentyl.Util || (Gentyl.Util = {})); })(Gentyl || (Gentyl = {}));