pax_global_header00006660000000000000000000000064133046445560014524gustar00rootroot0000000000000052 comment=db085349a0b3cfc255502013eeadc7c6b7b58cb3 unify-0.2.7/000077500000000000000000000000001330464455600126645ustar00rootroot00000000000000unify-0.2.7/.editorconfig000066400000000000000000000003111330464455600153340ustar00rootroot00000000000000root = true [*] charset = utf-8 end_of_line = lf insert_final_newline = true trim_trailing_whitespace = true indent_style = tab indent_size = 4 [*.{json,yml,md}] indent_style = space indent_size = 2 unify-0.2.7/.gitignore000066400000000000000000000001101330464455600146440ustar00rootroot00000000000000node_modules/* *.iml .idea *.sublime-* report/* coverage/* .AppleDouble unify-0.2.7/.travis.yml000066400000000000000000000005371330464455600150020ustar00rootroot00000000000000language: node_js node_js: - "6" - "8" - "10" env: - PHANTOM=true - PHANTOM=false matrix: exclude: - node_js: "6" env: PHANTOM=true - node_js: "8" env: PHANTOM=true before_script: if [ "$PHANTOM" == "true" ]; then export RUN_TEST="phantomjs tests/phantom.js"; else export RUN_TEST="npm test"; fi script: $RUN_TEST unify-0.2.7/AUTHORS000066400000000000000000000001031330464455600137260ustar00rootroot00000000000000Eugene Lazutkin (http://lazutkin.com/) unify-0.2.7/LICENSE000066400000000000000000000255431330464455600137020ustar00rootroot00000000000000This library is available under *either* the terms of the modified BSD license *or* the Academic Free License version 2.1. As a recipient of this work, you may choose which license to receive this code under. No external contributions are allowed under licenses which are fundamentally incompatible with the AFL or BSD licenses that this library is distributed under. The text of the AFL and BSD licenses is reproduced below. ------------------------------------------------------------------------------- The "New" BSD License: ********************** Copyright (c) 2005-2012, Eugene Lazutkin All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of Eugene Lazutkin nor the names of other contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ------------------------------------------------------------------------------- The Academic Free License, v. 2.1: ********************************** This Academic Free License (the "License") applies to any original work of authorship (the "Original Work") whose owner (the "Licensor") has placed the following notice immediately following the copyright notice for the Original Work: Licensed under the Academic Free License version 2.1 1) Grant of Copyright License. Licensor hereby grants You a world-wide, royalty-free, non-exclusive, perpetual, sublicenseable license to do the following: a) to reproduce the Original Work in copies; b) to prepare derivative works ("Derivative Works") based upon the Original Work; c) to distribute copies of the Original Work and Derivative Works to the public; d) to perform the Original Work publicly; and e) to display the Original Work publicly. 2) Grant of Patent License. Licensor hereby grants You a world-wide, royalty-free, non-exclusive, perpetual, sublicenseable license, under patent claims owned or controlled by the Licensor that are embodied in the Original Work as furnished by the Licensor, to make, use, sell and offer for sale the Original Work and Derivative Works. 3) Grant of Source Code License. The term "Source Code" means the preferred form of the Original Work for making modifications to it and all available documentation describing how to modify the Original Work. Licensor hereby agrees to provide a machine-readable copy of the Source Code of the Original Work along with each copy of the Original Work that Licensor distributes. Licensor reserves the right to satisfy this obligation by placing a machine-readable copy of the Source Code in an information repository reasonably calculated to permit inexpensive and convenient access by You for as long as Licensor continues to distribute the Original Work, and by publishing the address of that information repository in a notice immediately following the copyright notice that applies to the Original Work. 4) Exclusions From License Grant. Neither the names of Licensor, nor the names of any contributors to the Original Work, nor any of their trademarks or service marks, may be used to endorse or promote products derived from this Original Work without express prior written permission of the Licensor. Nothing in this License shall be deemed to grant any rights to trademarks, copyrights, patents, trade secrets or any other intellectual property of Licensor except as expressly stated herein. No patent license is granted to make, use, sell or offer to sell embodiments of any patent claims other than the licensed claims defined in Section 2. No right is granted to the trademarks of Licensor even if such marks are included in the Original Work. Nothing in this License shall be interpreted to prohibit Licensor from licensing under different terms from this License any Original Work that Licensor otherwise would have a right to license. 5) This section intentionally omitted. 6) Attribution Rights. You must retain, in the Source Code of any Derivative Works that You create, all copyright, patent or trademark notices from the Source Code of the Original Work, as well as any notices of licensing and any descriptive text identified therein as an "Attribution Notice." You must cause the Source Code for any Derivative Works that You create to carry a prominent Attribution Notice reasonably calculated to inform recipients that You have modified the Original Work. 7) Warranty of Provenance and Disclaimer of Warranty. Licensor warrants that the copyright in and to the Original Work and the patent rights granted herein by Licensor are owned by the Licensor or are sublicensed to You under the terms of this License with the permission of the contributor(s) of those copyrights and patent rights. Except as expressly stated in the immediately proceeding sentence, the Original Work is provided under this License on an "AS IS" BASIS and WITHOUT WARRANTY, either express or implied, including, without limitation, the warranties of NON-INFRINGEMENT, MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY OF THE ORIGINAL WORK IS WITH YOU. This DISCLAIMER OF WARRANTY constitutes an essential part of this License. No license to Original Work is granted hereunder except under this disclaimer. 8) Limitation of Liability. Under no circumstances and under no legal theory, whether in tort (including negligence), contract, or otherwise, shall the Licensor be liable to any person for any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or the use of the Original Work including, without limitation, damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses. This limitation of liability shall not apply to liability for death or personal injury resulting from Licensor's negligence to the extent applicable law prohibits such limitation. Some jurisdictions do not allow the exclusion or limitation of incidental or consequential damages, so this exclusion and limitation may not apply to You. 9) Acceptance and Termination. If You distribute copies of the Original Work or a Derivative Work, You must make a reasonable effort under the circumstances to obtain the express assent of recipients to the terms of this License. Nothing else but this License (or another written agreement between Licensor and You) grants You permission to create Derivative Works based upon the Original Work or to exercise any of the rights granted in Section 1 herein, and any attempt to do so except under the terms of this License (or another written agreement between Licensor and You) is expressly prohibited by U.S. copyright law, the equivalent laws of other countries, and by international treaty. Therefore, by exercising any of the rights granted to You in Section 1 herein, You indicate Your acceptance of this License and all of its terms and conditions. 10) Termination for Patent Action. This License shall terminate automatically and You may no longer exercise any of the rights granted to You by this License as of the date You commence an action, including a cross-claim or counterclaim, against Licensor or any licensee alleging that the Original Work infringes a patent. This termination provision shall not apply for an action alleging patent infringement by combinations of the Original Work with other software or hardware. 11) Jurisdiction, Venue and Governing Law. Any action or suit relating to this License may be brought only in the courts of a jurisdiction wherein the Licensor resides or in which Licensor conducts its primary business, and under the laws of that jurisdiction excluding its conflict-of-law provisions. The application of the United Nations Convention on Contracts for the International Sale of Goods is expressly excluded. Any use of the Original Work outside the scope of this License or after its termination shall be subject to the requirements and penalties of the U.S. Copyright Act, 17 U.S.C. § 101 et seq., the equivalent laws of other countries, and international treaty. This section shall survive the termination of this License. 12) Attorneys Fees. In any action to enforce the terms of this License or seeking damages relating thereto, the prevailing party shall be entitled to recover its costs and expenses, including, without limitation, reasonable attorneys' fees and costs incurred in connection with such action, including any appeal of such action. This section shall survive the termination of this License. 13) Miscellaneous. This License represents the complete agreement concerning the subject matter hereof. If any provision of this License is held to be unenforceable, such provision shall be reformed only to the extent necessary to make it enforceable. 14) Definition of "You" in This License. "You" throughout this License, whether in upper or lower case, means an individual or a legal entity exercising rights under, and complying with all of the terms of, this License. For legal entities, "You" includes any entity that controls, is controlled by, or is under common control with you. For purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. 15) Right to Use. You may use the Original Work in all ways not otherwise restricted or conditioned by this License or by law, and Licensor promises not to interfere with or be responsible for such uses by You. This license is Copyright (C) 2003-2004 Lawrence E. Rosen. All rights reserved. Permission is hereby granted to copy and distribute this license without modification. This license may not be modified without the express written permission of its copyright owner. unify-0.2.7/README.md000066400000000000000000000030061330464455600141420ustar00rootroot00000000000000# Unify [![Build status][travis-image]][travis-url] [![Dependencies][deps-image]][deps-url] [![devDependencies][dev-deps-image]][dev-deps-url] [![NPM version][npm-image]][npm-url] Unify: a unification tool with a deep equivalence and partitioning of objects. It is thoroughly documented in series of blog posts: * [Unification for JS](http://www.lazutkin.com/blog/2014/05/18/unification-for-js/). * [When to unify in JS](http://www.lazutkin.com/blog/2014/06/05/when-to-unify-in-js/). * [heya-unify: custom unification](http://www.lazutkin.com/blog/2014/06/10/heya-unify-custom-unification/). * [heya-unify: incomplete objects](http://www.lazutkin.com/blog/2014/06/18/heya-unify-incomplete-objects/). * [heya-unify: back to JS](http://www.lazutkin.com/blog/2014/07/11/heya-unify-back-to-js/). ## How to install If you plan to use it in your [node.js](http://nodejs.org) project install it like this: ``` npm install heya-unify ``` For your browser-based projects I suggest to use [volo.js](http://volojs.org): ``` volo install heya/unify heya-unify ``` [npm-image]: https://img.shields.io/npm/v/heya-unify.svg [npm-url]: https://npmjs.org/package/heya-unify [deps-image]: https://img.shields.io/david/heya/unify.svg [deps-url]: https://david-dm.org/heya/unify [dev-deps-image]: https://img.shields.io/david/dev/heya/unify.svg [dev-deps-url]: https://david-dm.org/heya/unify#info=devDependencies [travis-image]: https://img.shields.io/travis/heya/unify.svg [travis-url]: https://travis-ci.org/heya/unify unify-0.2.7/bower.json000066400000000000000000000012051330464455600146730ustar00rootroot00000000000000{ "name": "heya-unify", "description": "Unify: a unification tool with a deep equivalence and partitioning of objects.", "main": "main.js", "authors": [ "Eugene Lazutkin (http://lazutkin.com/)" ], "license": "BSD-3-Clause", "keywords": [ "unification", "equivalence", "comparison", "deep", "equivalence", "code", "generation", "code", "construction" ], "homepage": "https://github.com/heya/unify", "moduleType": [ "amd", "globals", "node" ], "ignore": [ "**/.*", "node_modules", "bower_components", "test", "tests" ] } unify-0.2.7/dist/000077500000000000000000000000001330464455600136275ustar00rootroot00000000000000unify-0.2.7/dist/main.js000066400000000000000000000344661330464455600151260ustar00rootroot00000000000000(function(_,f,g){g=window;g=g.heya||(g.heya={});g.unify=f();}) ([], function(){ "use strict"; // AnyVar var _ = {}; // Env function Env(){ this.variables = {}; this.values = {}; } Env.prototype = { declaredClass: "unify/Env", bindVar: function(name1, name2){ var vars = this.variables, u, t, k; if(vars.hasOwnProperty(name1)){ u = vars[name1]; if(vars.hasOwnProperty(name2)){ t = vars[name2]; for(k in t){ if(t.hasOwnProperty(k)){ vars[k] = u; u[k] = 1; } } }else{ vars[name2] = u; u[name2] = 1; } }else{ if(vars.hasOwnProperty(name2)){ u = vars[name1] = vars[name2]; u[name1] = 1; }else{ u = vars[name1] = vars[name2] = {}; u[name1] = u[name2] = 1; } } }, bindVal: function(name, val){ if(this.variables.hasOwnProperty(name)){ var names = this.variables[name]; for(var k in names){ if(names.hasOwnProperty(k)){ this.values[k] = val; } } }else{ this.values[name] = val; } } }; // Command function Command(f, l, r){ this.f = f; this.l = l; this.r = r; } Command.prototype.declaredClass = "unify/Command"; // Custom unifier function Unifier(){} Unifier.prototype.declaredClass = "unify/Unifier"; function isUnifier(x){ return x && x instanceof Unifier; } // Unifier should define a method: // unify(val, ls, rs, env): // val is a value we are unifying with // ls is a stack of left arguments // rs is a stack of right arguments corresponding to ls // env is an environment // the result should be true/false for success/failure // Var var unique = 0; function Var(name){ this.name = name || ("var" + unique++); } Var.prototype = Object.create(Unifier.prototype); Var.prototype.declaredClass = "unify/Var"; Var.prototype.bound = function(env){ return env.values.hasOwnProperty(this.name); }; Var.prototype.alias = function(name, env){ var t = env.variables[this.name]; return t && t[name]; }; Var.prototype.get = function(env){ return env.values[this.name]; }; Var.prototype.unify = function(val, ls, rs, env){ if(this.bound(env)){ ls.push(this.get(env)); rs.push(val); return true; } // the next case is taken care of in unify() directly // the case of unbound variable //if(val === _ || val === this){ // return true; //} if(val instanceof Var){ if(val.bound(env)){ env.bindVal(this.name, val.get(env)); }else{ env.bindVar(this.name, val.name); } return true; } env.bindVal(this.name, val); return true; }; function isVariable(x){ return x && x instanceof Var; } function variable(name){ return new Var(name); } // type wrapper function Wrap(type, o){ this.type = type; this.object = o; } Wrap.prototype = Object.create(Unifier.prototype); Wrap.prototype.declaredClass = "unify/Wrap"; Wrap.prototype.unify = function(val, ls, rs, env){ var ops; if(this.object instanceof Array){ // unify arrays if(!val) return false; if(val instanceof Array){ // with a naked array return unifyArrays(this.object, this.type, this, val, env.arrayType ? "open" : "exact", null, ls, rs, env); } if(val instanceof Wrap){ // with a wrapped array return val.object instanceof Array && unifyArrays(this.object, this.type, this, val.object, val.type, val, ls, rs, env); } return false; } // unify objects if(!val || val instanceof Array) return false; if(val instanceof Wrap){ // with a wrapped object return !(val.object instanceof Array) && unifyObjects(this.object, this.type, this, val.object, val.type, val, ls, rs, env); } // with a naked object return typeof val == "object" && unifyObjects(this.object, this.type, this, val, env.objectType ? "open" : "exact", null, ls, rs, env); }; function isWrapped(o){ return o && o instanceof Wrap; } function open(o){ return new Wrap("open", o); } function isOpen(o){ return o && o instanceof Wrap && o.type === "open"; } function soft(o){ return new Wrap("soft", o); } function isSoft(o){ return o && o instanceof Wrap && o.type === "soft"; } // registry of well-known constructors var registry = [ Array, unifyArray, Date, unifyDate, RegExp, unifyRegExp ]; var filters = []; function unifyArray(l, r, ls, rs, env){ if(!r || !(r instanceof Array) || !env.arrayType && l.length != r.length) return false; for(var i = 0, n = Math.min(l.length, r.length); i < n; ++i){ ls.push(l[i]); rs.push(r[i]); } return true; } function unifyDate(l, r, ls, rs, env){ return r && r instanceof Date && l.getTime() == r.getTime(); } function unifyRegExp(l, r, ls, rs, env){ return r && r instanceof RegExp && l.source == r.source && l.global == r.global && l.multiline == r.multiline && l.ignoreCase == r.ignoreCase; } // unification of arrays var arrayOps = { exact: { exact: { precheck: function(l, r){ return l.length == r.length; } }, open: { precheck: function(l, r){ return l.length >= r.length; } }, soft: { precheck: function(l, r){ return l.length >= r.length; }, fix: function(){ this.l.type = "exact"; } } }, open: { open: {}, soft: {} }, soft: { soft: { update: function(){ if(this.l.length > this.r.length){ this.r.push.apply(this.r, this.l.slice(this.r.length)); }else if(this.l.length < this.r.length){ this.l.push.apply(this.l, this.r.slice(this.l.length)); } } } } }; arrayOps.exact.exact.compare = arrayOps.exact.open.compare = arrayOps.exact.soft.compare = function(l, r, ls, rs){ for(var i = 0, n = r.length; i < n; ++i){ ls.push(l[i]); rs.push(r[i]); } }; arrayOps.open.open.compare = arrayOps.open.soft.compare = arrayOps.soft.soft.compare = function(l, r, ls, rs){ for(var i = 0, n = Math.min(l.length, r.length); i < n; ++i){ ls.push(l[i]); rs.push(r[i]); } }; arrayOps.exact.soft.update = arrayOps.open.soft.update = function(){ if(this.l.length > this.r.length){ this.r.push.apply(this.r, this.l.slice(this.r.length)); } }; function unifyArrays(l, lt, lm, r, rt, rm, ls, rs, env){ if(lt > rt){ var t = l; l = r; r = t; t = lm; lm = rm; rm = t; t = lt; lt = rt; rt = t; } var ops = arrayOps[lt][rt]; if(ops.precheck && !ops.precheck(l, r)) return false; if(ops.fix && rm) ls.push(new Command(ops.fix, rm)); if(ops.update && l.length != r.length) ls.push(new Command(ops.update, l, r)); ops.compare(l, r, ls, rs, env); return true; } // unification of objects var hasOwnProperty = Object.prototype.hasOwnProperty; var objectOps = { exact: { exact: { precheck: function(l, r){ if(typeof l.hasOwnProperty == 'function'){ if(typeof r.hasOwnProperty == 'function'){ for(var k in l){ if(l.hasOwnProperty(k) && !r.hasOwnProperty(k)) return false; } }else{ for(var k in l){ if(l.hasOwnProperty(k) && !hasOwnProperty.call(r, k)) return false; } } }else{ if(typeof r.hasOwnProperty == 'function'){ for(var k in l){ if(hasOwnProperty.call(l, k) && !r.hasOwnProperty(k)) return false; } }else{ for(var k in l){ if(hasOwnProperty.call(l, k) && !hasOwnProperty.call(r, k)) return false; } } } return true; } }, open: {}, soft: { fix: function(){ this.l.type = "exact"; } } }, open: { open: {}, soft: {} }, soft: { soft: { update: function(){ if(typeof this.l.hasOwnProperty == 'function'){ if(typeof this.r.hasOwnProperty == 'function'){ for(var k in this.l){ if(this.l.hasOwnProperty(k) && !this.r.hasOwnProperty(k)){ this.r[k] = this.l[k]; } } for(k in this.r){ if(this.r.hasOwnProperty(k) && !this.l.hasOwnProperty(k)){ this.l[k] = this.r[k]; } } }else{ for(var k in this.l){ if(this.l.hasOwnProperty(k) && !hasOwnProperty.call(this.r, k)){ this.r[k] = this.l[k]; } } for(k in this.r){ if(this.r.hasOwnProperty(k) && !hasOwnProperty.call(this.l, k)){ this.l[k] = this.r[k]; } } } }else{ if(typeof this.r.hasOwnProperty == 'function'){ for(var k in this.l){ if(hasOwnProperty.call(this.l, k) && !this.r.hasOwnProperty(k)){ this.r[k] = this.l[k]; } } for(k in this.r){ if(hasOwnProperty.call(this.rk) && !this.l.hasOwnProperty(k)){ this.l[k] = this.r[k]; } } }else{ if(typeof this.r.hasOwnProperty == 'function'){ for(var k in this.l){ if(hasOwnProperty.call(this.l, k) && !hasOwnProperty.call(this.r, k)){ this.r[k] = this.l[k]; } } for(k in this.r){ if(hasOwnProperty.call(this.rk) && !hasOwnProperty.call(this.l, k)){ this.l[k] = this.r[k]; } } } } } } } } }; objectOps.exact.exact.compare = objectOps.exact.open.compare = objectOps.exact.soft.compare = function(l, r, ls, rs){ if(typeof l.hasOwnProperty == 'function'){ if(typeof r.hasOwnProperty == 'function'){ for(var k in r){ if(r.hasOwnProperty(k)){ if(!l.hasOwnProperty(k)){ return false; } ls.push(l[k]); rs.push(r[k]) } } }else{ for(var k in r){ if(hasOwnProperty.call(r, k)){ if(!l.hasOwnProperty(k)){ return false; } ls.push(l[k]); rs.push(r[k]) } } } }else{ if(typeof r.hasOwnProperty == 'function'){ for(var k in r){ if(r.hasOwnProperty(k)){ if(!hasOwnProperty.call(l, k)){ return false; } ls.push(l[k]); rs.push(r[k]) } } }else{ for(var k in r){ if(hasOwnProperty.call(r, k)){ if(!hasOwnProperty.call(l, k)){ return false; } ls.push(l[k]); rs.push(r[k]) } } } } return true; }; objectOps.open.open.compare = objectOps.open.soft.compare = objectOps.soft.soft.compare = function(l, r, ls, rs){ if(typeof l.hasOwnProperty == 'function'){ if(typeof r.hasOwnProperty == 'function'){ for(var k in r){ if(r.hasOwnProperty(k) && l.hasOwnProperty(k)){ ls.push(l[k]); rs.push(r[k]) } } }else{ for(var k in r){ if(hasOwnProperty.call(r, k) && l.hasOwnProperty(k)){ ls.push(l[k]); rs.push(r[k]) } } } }else{ if(typeof r.hasOwnProperty == 'function'){ for(var k in r){ if(r.hasOwnProperty(k) && hasOwnProperty.call(l, k)){ ls.push(l[k]); rs.push(r[k]) } } }else{ for(var k in r){ if(hasOwnProperty.call(r, k) && hasOwnProperty.call(l, k)){ ls.push(l[k]); rs.push(r[k]) } } } } return true; }; objectOps.exact.soft.update = objectOps.open.soft.update = function(){ if(typeof this.l.hasOwnProperty == 'function'){ if(typeof this.r.hasOwnProperty == 'function'){ for(var k in this.l){ if(this.l.hasOwnProperty(k) && !this.r.hasOwnProperty(k)){ this.r[k] = this.l[k]; } } }else{ for(var k in this.l){ if(this.l.hasOwnProperty(k) && !hasOwnProperty.call(this.r, k)){ this.r[k] = this.l[k]; } } } }else{ if(typeof this.r.hasOwnProperty == 'function'){ for(var k in this.l){ if(hasOwnProperty.call(this.l, k) && !this.r.hasOwnProperty(k)){ this.r[k] = this.l[k]; } } }else{ for(var k in this.l){ if(hasOwnProperty.call(this.l, k) && !hasOwnProperty.call(this.r, k)){ this.r[k] = this.l[k]; } } } } }; function unifyObjects(l, lt, lm, r, rt, rm, ls, rs, env){ if(lt > rt){ var t = l; l = r; r = t; t = lm; lm = rm; rm = t; t = lt; lt = rt; rt = t; } var ops = objectOps[lt][rt]; if(ops.precheck && !ops.precheck(l, r)) return false; if(ops.fix && rm) ls.push(new Command(ops.fix, rm)); if(ops.update) ls.push(new Command(ops.update, l, r)); return ops.compare(l, r, ls, rs, env); } // unification function unify(l, r, env){ env = env || new Env(); var ls = [l], rs = [r], objectType = env.objectType ? "open" : "exact"; main: while(ls.length){ // perform a command, or extract a pair l = ls.pop(); if(l && l instanceof Command){ l.f(); continue; } r = rs.pop(); // direct unity or anyvar if(l === r || l === _ || r === _){ continue; } // process variables (variables have priority) if(l instanceof Var){ if(l.unify(r, ls, rs, env)) continue; return null; } if(r instanceof Var){ if(r.unify(l, ls, rs, env)) continue; return null; } // invoke custom unifiers if(l instanceof Unifier){ if(l.unify(r, ls, rs, env)) continue; return null; } if(r instanceof Unifier){ if(r.unify(l, ls, rs, env)) continue; return null; } // check rough types if(typeof l != typeof r){ return null; } // special case: NaN if(typeof l == "number" && isNaN(l) && isNaN(r)){ continue; } // cut off impossible combinations if(typeof l != "object" && typeof l != "function" || !l || !r){ return null; } // process registered constructors var registry = unify.registry; for(var i = 0, len = registry.length; i < len; i += 2){ if(l instanceof registry[i] || r instanceof registry[i]){ if(registry[i + 1](l, r, ls, rs, env)) continue main; return null; } } // process registered filters registry = unify.filters; for(i = 0, len = registry.length; i < len; i += 2){ if(registry[i](l, r)){ if(registry[i + 1](l, r, ls, rs, env)) continue main; return null; } } // process naked objects if(!unifyObjects(l, objectType, null, r, objectType, null, ls, rs, env)) return null; } return env; } // exports unify._ = unify.any = _; unify.registry = registry; unify.filters = filters; unify.Env = Env; unify.Unifier = Unifier; unify.Variable = Var; unify.variable = variable; unify.open = open; unify.soft = soft; unify.isUnifier = isUnifier; unify.isVariable = isVariable; unify.isWrapped = isWrapped; unify.isOpen = isOpen; unify.isSoft = isSoft; return unify; }); unify-0.2.7/dist/unifiers/000077500000000000000000000000001330464455600154535ustar00rootroot00000000000000unify-0.2.7/dist/unifiers/match.js000066400000000000000000000006131330464455600171050ustar00rootroot00000000000000(function(_,f,g){g=window.heya.unify;g=g.unifiers||(g.unifiers={});g.match=f(window.heya.unify);}) (["../main"], function(unify){ "use strict"; function Match(f){ this.f = f; } Match.prototype = Object.create(unify.Unifier.prototype); Match.prototype.unify = function(val, ls, rs, env){ return this.f(val, ls, rs, env); }; return function match(f){ return new Match(f); }; }); unify-0.2.7/dist/unifiers/matchInstanceOf.js000066400000000000000000000011051330464455600210540ustar00rootroot00000000000000(function(_,f,g){g=window.heya.unify;g=g.unifiers||(g.unifiers={});g.matchInstanceOf=f(window.heya.unify);}) (["../main"], function(unify){ "use strict"; function MatchInstanceOf(types){ this.types = types instanceof Array ? types : [types]; } MatchInstanceOf.prototype = Object.create(unify.Unifier.prototype); MatchInstanceOf.prototype.unify = function(val, ls, rs){ return val && !unify.isVariable(val) && this.types.some(function(type){ return val instanceof type; }); }; return function matchInstanceOf(types){ return new MatchInstanceOf(types); }; }); unify-0.2.7/dist/unifiers/matchString.js000066400000000000000000000016021330464455600202730ustar00rootroot00000000000000(function(_,f,g){g=window.heya.unify;g=g.unifiers||(g.unifiers={});g.matchString=f(window.heya.unify);}) (["../main"], function(unify){ "use strict"; function MatchString(regexp, matches, props){ this.regexp = regexp; this.matches = matches; this.props = props; } MatchString.prototype = Object.create(unify.Unifier.prototype); MatchString.prototype.unify = function(val, ls, rs){ if(unify.isVariable(val)){ // cannot match with an unbound variable return false; } var result = this.regexp.exec("" + val); if(result){ if(this.matches){ ls.push(this.matches); rs.push(Array.prototype.slice.call(result, 0)); } if(this.props){ ls.push(this.props); rs.push({index: result.index, input: result.input}); } } return result; }; return function matchString(regexp, matches, props){ return new MatchString(regexp, matches, props); }; }); unify-0.2.7/dist/unifiers/matchTypeOf.js000066400000000000000000000010061330464455600202310ustar00rootroot00000000000000(function(_,f,g){g=window.heya.unify;g=g.unifiers||(g.unifiers={});g.matchTypeOf=f(window.heya.unify);}) (["../main"], function(unify){ "use strict"; function MatchTypeOf(types){ this.types = types instanceof Array ? types : [types]; } MatchTypeOf.prototype = Object.create(unify.Unifier.prototype); MatchTypeOf.prototype.unify = function(val, ls, rs){ return !unify.isVariable(val) && this.types.indexOf(typeof val) >= 0; }; return function matchTypeOf(types){ return new MatchTypeOf(types); }; }); unify-0.2.7/dist/unifiers/ref.js000066400000000000000000000012371330464455600165700ustar00rootroot00000000000000(function(_,f,g){g=window.heya.unify;g=g.unifiers||(g.unifiers={});g.ref=f(window.heya.unify);}) (["../main"], function(unify){ "use strict"; var Var = unify.Variable; function Ref(variable, value){ this.variable = typeof variable == "string" ? new Var(variable) : variable; this.value = value; Var.call(this, this.variable.name); } Ref.prototype = Object.create(Var.prototype); Ref.prototype.declaredClass = "unify/Ref"; Ref.prototype.unify = function(val, ls, rs, env){ ls.push(this.value, this.variable); rs.push(val, val); return true; }; function ref(variable, value){ return new Ref(variable, value); } ref.Ref = Ref; return ref; }); unify-0.2.7/dist/utils/000077500000000000000000000000001330464455600147675ustar00rootroot00000000000000unify-0.2.7/dist/utils/assemble.js000066400000000000000000000060271330464455600171250ustar00rootroot00000000000000(function(_,f,m){m={};m.id=m.filename="utils/assemble.js";window.heya.unify.utils.assemble=f(m,window.heya.ice.assert,window.heya.unify,window.heya.unify.utils.walk);}) (["module", "heya-ice/assert", "../main", "./walk"], function(module, ice, unify, walk){ "use strict"; ice = ice.specialize(module); var empty = {}; function postArray(context){ var stackOut = context.stackOut, s = this.s, l = s.length, i = 0, j = stackOut.length - 1, t; main: { for(; i < l; ++i){ if(s.hasOwnProperty(i)){ t = stackOut[j--]; if(typeof t == "number" && isNaN(t) ? typeof s[i] == "number" && !isNaN(s[i]) : s[i] !== t){ break main; } } } l = stackOut.length - 1 - j; if(l){ stackOut.splice(-l, l, s); }else{ stackOut.push(s); } return; } t = []; for(i = 0; i < l; ++i){ if(s.hasOwnProperty(i)){ t[i] = stackOut.pop(); } } stackOut.push(t); } function postObject(context){ var stackOut = context.stackOut, s = this.s, k, j = stackOut.length - 1, t; main: { for(k in s){ if(s.hasOwnProperty(k)){ t = stackOut[j--]; if(typeof t == "number" && isNaN(t) ? typeof s[k] == "number" && !isNaN(s[k]) : s[k] !== t){ break main; } } } var l = stackOut.length - 1 - j; if(l){ stackOut.splice(-l, l, s); }else{ stackOut.push(s); } return; } t = {}; for(k in s){ if(s.hasOwnProperty(k)){ t[k] = stackOut.pop(); } } stackOut.push(t); } function processObject(val, context){ if(val === unify._){ context.stackOut.push(val); }else{ var stack = context.stack; stack.push(new (walk.Command)(postObject, val)); for(var k in val){ if(val.hasOwnProperty(k)){ stack.push(val[k]); } } } } function processOther(val, context){ context.stackOut.push(val); } var registry = [ walk.Command, function processCommand(val, context){ val.f(context); }, Array, function processArray(val, context){ var stack = context.stack; stack.push(new (walk.Command)(postArray, val)); for(var i = 0, l = val.length; i < l; ++i){ if(val.hasOwnProperty(i)){ stack.push(val[i]); } } }, unify.Variable, function processVariable(val, context){ var env = context.env; if(val.bound(env)){ context.stack.push(val.get(env)); }else{ context.stackOut.push(val); } }, unify.Unifier, processOther, Date, processOther, RegExp, processOther ], filters = []; function assemble(source, env, opt){ opt = opt || empty; var context = opt.context || {}, stackOut = []; context.stackOut = stackOut; context.env = env; walk(source, { processObject: opt.processObject || processObject, processOther: opt.processOther || processOther, registry: opt.registry || assemble.registry, filters: opt.filters || assemble.filters, context: context }); ice.assert(stackOut.length == 1); return stackOut[0]; } assemble.registry = registry; assemble.filters = filters; return assemble; }); unify-0.2.7/dist/utils/clone.js000066400000000000000000000051511330464455600164270ustar00rootroot00000000000000(function(_,f,m){m={};m.id=m.filename="utils/clone.js";window.heya.unify.utils.clone=f(m,window.heya.ice.assert,window.heya.unify,window.heya.unify.utils.walk);}) (["module", "heya-ice/assert", "../main", "./walk"], function(module, ice, unify, walk){ "use strict"; ice = ice.specialize(module); var empty = {}; function postArray(context){ var stackOut = context.stackOut, t = []; for(var i = 0, s = this.s, l = s.length; i < l; ++i){ if(s.hasOwnProperty(i)){ t[i] = stackOut.pop(); } } stackOut.push(t); } function postObject(context){ var stackOut = context.stackOut, t = {}, s = this.s; for(var k in s){ if(s.hasOwnProperty(k)){ t[k] = stackOut.pop(); } } stackOut.push(t); } function processObject(val, context){ if(val === unify._){ context.stackOut.push(s); }else{ var stack = context.stack; stack.push(new (walk.Command)(postObject, val)); for(var k in val){ if(val.hasOwnProperty(k)){ stack.push(val[k]); } } } } var registry = [ walk.Command, function processCommand(val, context){ val.f(context); }, Array, function processArray(val, context){ var stack = context.stack; stack.push(new (walk.Command)(postArray, val)); for(var i = 0, l = val.length; i < l; ++i){ if(val.hasOwnProperty(i)){ stack.push(val[i]); } } }, unify.Variable, function processVariable(val, context){ var env = context.env; if(val.bound(env)){ context.stack.push(val.get(env)); }else{ context.stackOut.push(val); } }, unify.Unifier, function processAsValue(val, context){ context.stackOut.push(val); }, Date, function processDate(val, context){ context.stackOut.push(new Date(val.getTime())); }, RegExp, function processRegExp(val, context){ context.stackOut.push(new RegExp(val.source, (val.global ? "g" : "") + (val.multiline ? "m" : "") + (val.ignoreCase ? "i" : "") )); } ], filters = []; function processOther(val, context){ context.stackOut.push(val); } function clone(source, env, opt){ var stackOut = []; opt = opt || empty; var context = opt.context || {}, stackOut = []; context.stackOut = stackOut; context.env = env; walk(source, { processObject: opt.processObject || processObject, processOther: opt.processOther || processOther, registry: opt.registry || clone.registry, filters: opt.filters || clone.filters, context: context }); ice.assert(stackOut.length == 1); return stackOut[0]; } clone.registry = registry; clone.filters = filters; return clone; }); unify-0.2.7/dist/utils/deref.js000066400000000000000000000047421330464455600164210ustar00rootroot00000000000000(function(_,f,m){m={};m.id=m.filename="utils/deref.js";window.heya.unify.utils.deref=f(m,window.heya.ice.assert,window.heya.unify,window.heya.unify.utils.walk);}) (["module", "heya-ice/assert", "../main", "./walk"], function(module, ice, unify, walk){ "use strict"; ice = ice.specialize(module); var empty = {}; function postArray(context){ var stackOut = context.stackOut, s = this.s, l = s.length, i = 0, j = stackOut.length - 1, t; for(; i < l; ++i){ if(s.hasOwnProperty(i)){ s[i] = stackOut[j--]; } } l = stackOut.length - 1 - j; if(l){ stackOut.splice(-l, l, s); }else{ stackOut.push(s); } } function postObject(context){ var stackOut = context.stackOut, s = this.s, k, j = stackOut.length - 1, t; for(k in s){ if(s.hasOwnProperty(k)){ s[k] = stackOut[j--]; } } var l = stackOut.length - 1 - j; if(l){ stackOut.splice(-l, l, s); }else{ stackOut.push(s); } } function processObject(val, context){ if(val === unify._){ context.stackOut.push(val); }else{ var stack = context.stack; stack.push(new (walk.Command)(postObject, val)); for(var k in val){ if(val.hasOwnProperty(k)){ stack.push(val[k]); } } } } function processOther(val, context){ context.stackOut.push(val); } var registry = [ walk.Command, function processCommand(val, context){ val.f(context); }, Array, function processArray(val, context){ var stack = context.stack; stack.push(new (walk.Command)(postArray, val)); for(var i = 0, l = val.length; i < l; ++i){ if(val.hasOwnProperty(i)){ stack.push(val[i]); } } }, unify.Variable, function processVariable(val, context){ var env = context.env; if(val.bound(env)){ context.stack.push(val.get(env)); }else{ context.stackOut.push(val); } }, unify.Unifier, processOther, Date, processOther, RegExp, processOther ], filters = []; function deref(source, env, opt){ opt = opt || empty; var context = opt.context || {}, stackOut = []; context.stackOut = stackOut; context.env = env; walk(source, { processObject: opt.processObject || processObject, processOther: opt.processOther || processOther, registry: opt.registry || deref.registry, filters: opt.filters || deref.filters, context: context }); ice.assert(stackOut.length == 1); return stackOut[0]; } deref.registry = registry; deref.filters = filters; return deref; }); unify-0.2.7/dist/utils/preprocess.js000066400000000000000000000045671330464455600175260ustar00rootroot00000000000000(function(_,f,m){m={};m.id=m.filename="utils/preprocess.js";window.heya.unify.utils.preprocess=f(m,window.heya.ice.assert,window.heya.unify,window.heya.unify.utils.walk);}) (["module", "heya-ice/assert", "../main", "./walk"], function(module, ice, unify, walk){ "use strict"; ice = ice.specialize(module); var empty = {}; function postArray(context){ var stackOut = context.stackOut, wrapArray = context.wrapArray, t = []; for(var i = 0, s = this.s, l = s.length; i < l; ++i){ if(s.hasOwnProperty(i)){ t[i] = stackOut.pop(); } } stackOut.push(wrapArray ? wrapArray(t) : t); } function postObject(context){ var stackOut = context.stackOut, wrapObject = context.wrapObject, t = {}, s = this.s; for(var k in s){ if(s.hasOwnProperty(k)){ t[k] = stackOut.pop(); } } stackOut.push(wrapObject ? wrapObject(t) : t); } function processObject(val, context){ if(val === unify._){ context.stackOut.push(val); }else{ var stack = context.stack; stack.push(new (walk.Command)(postObject, val)); for(var k in val){ if(val.hasOwnProperty(k)){ stack.push(val[k]); } } } } function processOther(val, context){ context.stackOut.push(val); } var registry = [ walk.Command, function processCommand(val, context){ val.f(context); }, Array, function processArray(val, context){ var stack = context.stack; stack.push(new (walk.Command)(postArray, val)); for(var i = 0, l = val.length; i < l; ++i){ if(val.hasOwnProperty(i)){ stack.push(val[i]); } } }, unify.Variable, processOther, unify.Unifier, processOther, Date, processOther, RegExp, processOther ], filters = []; function preprocess(source, nonExactObjects, nonExactArrays, opt){ opt = opt || empty; var context = opt.context || {}, stackOut = []; context.stackOut = stackOut; context.wrapObject = nonExactObjects && unify.open; context.wrapArray = nonExactArrays && unify.open; walk(source, { processObject: opt.processObject || processObject, processOther: opt.processOther || processOther, registry: opt.registry || preprocess.registry, filters: opt.filters || preprocess.filters, context: context }); ice.assert(stackOut.length == 1); return stackOut[0]; } preprocess.registry = registry; preprocess.filters = filters; return preprocess; }); unify-0.2.7/dist/utils/replace.js000066400000000000000000000005621330464455600167430ustar00rootroot00000000000000(function(_,f,g){g=window;g=g.heya||(g.heya={});g=g.unify||(g.unify={});g=g.utils||(g.utils={});g.replace=f();}) ([], function(){ "use strict"; return function replace(tmpl, env){ return tmpl.replace(/\$+\{([^\}\s\r\n]+)\}/g, function(match, name){ if(match.length - name.length > 3){ return match.substring(1); } return env.values[name]; }); }; }); unify-0.2.7/dist/utils/walk.js000066400000000000000000000032751330464455600162720ustar00rootroot00000000000000(function(_,f,g){g=window;g=g.heya||(g.heya={});g=g.unify||(g.unify={});g=g.utils||(g.utils={});g.walk=f();}) ([], function(){ "use strict"; var empty = {}; function nop(){} function Command(f, s){ this.f = f; this.s = s; } function processCommand(val, context){ val.f(context); } function processObject(val, context){ var stack = context.stack; for(var k in val){ if(val.hasOwnProperty(k)){ stack.push(val[k]); } } } function processArray(val, context){ var stack = context.stack; for(var i = 0, l = val.length; i < l; ++i){ if(val.hasOwnProperty(i)){ stack.push(val[i]); } } } var defaultRegistry = [ Command, processCommand, Array, processArray, Date, nop, RegExp, nop ], defaultFilters = []; function walk(o, opt){ // non-recursive stack-based walk about an object tree opt = opt || empty; var doObject = opt.processObject || processObject, doOther = opt.processOther || nop, registry = opt.registry || defaultRegistry, filters = opt.filters || defaultFilters, context = opt.context || {}, stack = [o]; context.stack = stack; main: while(stack.length){ o = stack.pop(); if(o && typeof o == "object"){ // process registered constructors for(var i = 0; i < registry.length; i += 2){ if(o instanceof registry[i]){ registry[i + 1](o, context); continue main; } } // process registered filters for(i = 0; i < filters.length; ++i){ if(filters[i](o, context)){ continue main; } } // process naked objects doObject(o, context); continue; } doOther(o, context); } } walk.Command = Command; return walk; }); unify-0.2.7/main.js000066400000000000000000000346511330464455600141570ustar00rootroot00000000000000/* UMD.define */ (typeof define=="function"&&define||function(d,f,m){m={module:module,require:require};module.exports=f.apply(null,d.map(function(n){return m[n]||require(n)}))}) ([], function(){ "use strict"; // AnyVar var _ = {}; // Env function Env(){ this.variables = {}; this.values = {}; } Env.prototype = { declaredClass: "unify/Env", bindVar: function(name1, name2){ var vars = this.variables, u, t, k; if(vars.hasOwnProperty(name1)){ u = vars[name1]; if(vars.hasOwnProperty(name2)){ t = vars[name2]; for(k in t){ if(t.hasOwnProperty(k)){ vars[k] = u; u[k] = 1; } } }else{ vars[name2] = u; u[name2] = 1; } }else{ if(vars.hasOwnProperty(name2)){ u = vars[name1] = vars[name2]; u[name1] = 1; }else{ u = vars[name1] = vars[name2] = {}; u[name1] = u[name2] = 1; } } }, bindVal: function(name, val){ if(this.variables.hasOwnProperty(name)){ var names = this.variables[name]; for(var k in names){ if(names.hasOwnProperty(k)){ this.values[k] = val; } } }else{ this.values[name] = val; } } }; // Command function Command(f, l, r){ this.f = f; this.l = l; this.r = r; } Command.prototype.declaredClass = "unify/Command"; // Custom unifier function Unifier(){} Unifier.prototype.declaredClass = "unify/Unifier"; function isUnifier(x){ return x && x instanceof Unifier; } // Unifier should define a method: // unify(val, ls, rs, env): // val is a value we are unifying with // ls is a stack of left arguments // rs is a stack of right arguments corresponding to ls // env is an environment // the result should be true/false for success/failure // Var var unique = 0; function Var(name){ this.name = name || ("var" + unique++); } Var.prototype = Object.create(Unifier.prototype); Var.prototype.declaredClass = "unify/Var"; Var.prototype.bound = function(env){ return env.values.hasOwnProperty(this.name); }; Var.prototype.alias = function(name, env){ var t = env.variables[this.name]; return t && t[name]; }; Var.prototype.get = function(env){ return env.values[this.name]; }; Var.prototype.unify = function(val, ls, rs, env){ if(this.bound(env)){ ls.push(this.get(env)); rs.push(val); return true; } // the next case is taken care of in unify() directly // the case of unbound variable //if(val === _ || val === this){ // return true; //} if(val instanceof Var){ if(val.bound(env)){ env.bindVal(this.name, val.get(env)); }else{ env.bindVar(this.name, val.name); } return true; } env.bindVal(this.name, val); return true; }; function isVariable(x){ return x && x instanceof Var; } function variable(name){ return new Var(name); } // type wrapper function Wrap(type, o){ this.type = type; this.object = o; } Wrap.prototype = Object.create(Unifier.prototype); Wrap.prototype.declaredClass = "unify/Wrap"; Wrap.prototype.unify = function(val, ls, rs, env){ var ops; if(this.object instanceof Array){ // unify arrays if(!val) return false; if(val instanceof Array){ // with a naked array return unifyArrays(this.object, this.type, this, val, env.arrayType ? "open" : "exact", null, ls, rs, env); } if(val instanceof Wrap){ // with a wrapped array return val.object instanceof Array && unifyArrays(this.object, this.type, this, val.object, val.type, val, ls, rs, env); } return false; } // unify objects if(!val || val instanceof Array) return false; if(val instanceof Wrap){ // with a wrapped object return !(val.object instanceof Array) && unifyObjects(this.object, this.type, this, val.object, val.type, val, ls, rs, env); } // with a naked object return typeof val == "object" && unifyObjects(this.object, this.type, this, val, env.objectType ? "open" : "exact", null, ls, rs, env); }; function isWrapped(o){ return o && o instanceof Wrap; } function open(o){ return new Wrap("open", o); } function isOpen(o){ return o && o instanceof Wrap && o.type === "open"; } function soft(o){ return new Wrap("soft", o); } function isSoft(o){ return o && o instanceof Wrap && o.type === "soft"; } // registry of well-known constructors var registry = [ Array, unifyArray, Date, unifyDate, RegExp, unifyRegExp ]; var filters = []; function unifyArray(l, r, ls, rs, env){ if(!r || !(r instanceof Array) || !env.arrayType && l.length != r.length) return false; for(var i = 0, n = Math.min(l.length, r.length); i < n; ++i){ ls.push(l[i]); rs.push(r[i]); } return true; } function unifyDate(l, r, ls, rs, env){ return r && r instanceof Date && l.getTime() == r.getTime(); } function unifyRegExp(l, r, ls, rs, env){ return r && r instanceof RegExp && l.source == r.source && l.global == r.global && l.multiline == r.multiline && l.ignoreCase == r.ignoreCase; } // unification of arrays var arrayOps = { exact: { exact: { precheck: function(l, r){ return l.length == r.length; } }, open: { precheck: function(l, r){ return l.length >= r.length; } }, soft: { precheck: function(l, r){ return l.length >= r.length; }, fix: function(){ this.l.type = "exact"; } } }, open: { open: {}, soft: {} }, soft: { soft: { update: function(){ if(this.l.length > this.r.length){ this.r.push.apply(this.r, this.l.slice(this.r.length)); }else if(this.l.length < this.r.length){ this.l.push.apply(this.l, this.r.slice(this.l.length)); } } } } }; arrayOps.exact.exact.compare = arrayOps.exact.open.compare = arrayOps.exact.soft.compare = function(l, r, ls, rs){ for(var i = 0, n = r.length; i < n; ++i){ ls.push(l[i]); rs.push(r[i]); } }; arrayOps.open.open.compare = arrayOps.open.soft.compare = arrayOps.soft.soft.compare = function(l, r, ls, rs){ for(var i = 0, n = Math.min(l.length, r.length); i < n; ++i){ ls.push(l[i]); rs.push(r[i]); } }; arrayOps.exact.soft.update = arrayOps.open.soft.update = function(){ if(this.l.length > this.r.length){ this.r.push.apply(this.r, this.l.slice(this.r.length)); } }; function unifyArrays(l, lt, lm, r, rt, rm, ls, rs, env){ if(lt > rt){ var t = l; l = r; r = t; t = lm; lm = rm; rm = t; t = lt; lt = rt; rt = t; } var ops = arrayOps[lt][rt]; if(ops.precheck && !ops.precheck(l, r)) return false; if(ops.fix && rm) ls.push(new Command(ops.fix, rm)); if(ops.update && l.length != r.length) ls.push(new Command(ops.update, l, r)); ops.compare(l, r, ls, rs, env); return true; } // unification of objects var hasOwnProperty = Object.prototype.hasOwnProperty; var objectOps = { exact: { exact: { precheck: function(l, r){ if(typeof l.hasOwnProperty == 'function'){ if(typeof r.hasOwnProperty == 'function'){ for(var k in l){ if(l.hasOwnProperty(k) && !r.hasOwnProperty(k)) return false; } }else{ for(var k in l){ if(l.hasOwnProperty(k) && !hasOwnProperty.call(r, k)) return false; } } }else{ if(typeof r.hasOwnProperty == 'function'){ for(var k in l){ if(hasOwnProperty.call(l, k) && !r.hasOwnProperty(k)) return false; } }else{ for(var k in l){ if(hasOwnProperty.call(l, k) && !hasOwnProperty.call(r, k)) return false; } } } return true; } }, open: {}, soft: { fix: function(){ this.l.type = "exact"; } } }, open: { open: {}, soft: {} }, soft: { soft: { update: function(){ if(typeof this.l.hasOwnProperty == 'function'){ if(typeof this.r.hasOwnProperty == 'function'){ for(var k in this.l){ if(this.l.hasOwnProperty(k) && !this.r.hasOwnProperty(k)){ this.r[k] = this.l[k]; } } for(k in this.r){ if(this.r.hasOwnProperty(k) && !this.l.hasOwnProperty(k)){ this.l[k] = this.r[k]; } } }else{ for(var k in this.l){ if(this.l.hasOwnProperty(k) && !hasOwnProperty.call(this.r, k)){ this.r[k] = this.l[k]; } } for(k in this.r){ if(this.r.hasOwnProperty(k) && !hasOwnProperty.call(this.l, k)){ this.l[k] = this.r[k]; } } } }else{ if(typeof this.r.hasOwnProperty == 'function'){ for(var k in this.l){ if(hasOwnProperty.call(this.l, k) && !this.r.hasOwnProperty(k)){ this.r[k] = this.l[k]; } } for(k in this.r){ if(hasOwnProperty.call(this.rk) && !this.l.hasOwnProperty(k)){ this.l[k] = this.r[k]; } } }else{ if(typeof this.r.hasOwnProperty == 'function'){ for(var k in this.l){ if(hasOwnProperty.call(this.l, k) && !hasOwnProperty.call(this.r, k)){ this.r[k] = this.l[k]; } } for(k in this.r){ if(hasOwnProperty.call(this.rk) && !hasOwnProperty.call(this.l, k)){ this.l[k] = this.r[k]; } } } } } } } } }; objectOps.exact.exact.compare = objectOps.exact.open.compare = objectOps.exact.soft.compare = function(l, r, ls, rs){ if(typeof l.hasOwnProperty == 'function'){ if(typeof r.hasOwnProperty == 'function'){ for(var k in r){ if(r.hasOwnProperty(k)){ if(!l.hasOwnProperty(k)){ return false; } ls.push(l[k]); rs.push(r[k]) } } }else{ for(var k in r){ if(hasOwnProperty.call(r, k)){ if(!l.hasOwnProperty(k)){ return false; } ls.push(l[k]); rs.push(r[k]) } } } }else{ if(typeof r.hasOwnProperty == 'function'){ for(var k in r){ if(r.hasOwnProperty(k)){ if(!hasOwnProperty.call(l, k)){ return false; } ls.push(l[k]); rs.push(r[k]) } } }else{ for(var k in r){ if(hasOwnProperty.call(r, k)){ if(!hasOwnProperty.call(l, k)){ return false; } ls.push(l[k]); rs.push(r[k]) } } } } return true; }; objectOps.open.open.compare = objectOps.open.soft.compare = objectOps.soft.soft.compare = function(l, r, ls, rs){ if(typeof l.hasOwnProperty == 'function'){ if(typeof r.hasOwnProperty == 'function'){ for(var k in r){ if(r.hasOwnProperty(k) && l.hasOwnProperty(k)){ ls.push(l[k]); rs.push(r[k]) } } }else{ for(var k in r){ if(hasOwnProperty.call(r, k) && l.hasOwnProperty(k)){ ls.push(l[k]); rs.push(r[k]) } } } }else{ if(typeof r.hasOwnProperty == 'function'){ for(var k in r){ if(r.hasOwnProperty(k) && hasOwnProperty.call(l, k)){ ls.push(l[k]); rs.push(r[k]) } } }else{ for(var k in r){ if(hasOwnProperty.call(r, k) && hasOwnProperty.call(l, k)){ ls.push(l[k]); rs.push(r[k]) } } } } return true; }; objectOps.exact.soft.update = objectOps.open.soft.update = function(){ if(typeof this.l.hasOwnProperty == 'function'){ if(typeof this.r.hasOwnProperty == 'function'){ for(var k in this.l){ if(this.l.hasOwnProperty(k) && !this.r.hasOwnProperty(k)){ this.r[k] = this.l[k]; } } }else{ for(var k in this.l){ if(this.l.hasOwnProperty(k) && !hasOwnProperty.call(this.r, k)){ this.r[k] = this.l[k]; } } } }else{ if(typeof this.r.hasOwnProperty == 'function'){ for(var k in this.l){ if(hasOwnProperty.call(this.l, k) && !this.r.hasOwnProperty(k)){ this.r[k] = this.l[k]; } } }else{ for(var k in this.l){ if(hasOwnProperty.call(this.l, k) && !hasOwnProperty.call(this.r, k)){ this.r[k] = this.l[k]; } } } } }; function unifyObjects(l, lt, lm, r, rt, rm, ls, rs, env){ if(lt > rt){ var t = l; l = r; r = t; t = lm; lm = rm; rm = t; t = lt; lt = rt; rt = t; } var ops = objectOps[lt][rt]; if(ops.precheck && !ops.precheck(l, r)) return false; if(ops.fix && rm) ls.push(new Command(ops.fix, rm)); if(ops.update) ls.push(new Command(ops.update, l, r)); return ops.compare(l, r, ls, rs, env); } // unification function unify(l, r, env){ env = env || new Env(); var ls = [l], rs = [r], objectType = env.objectType ? "open" : "exact"; main: while(ls.length){ // perform a command, or extract a pair l = ls.pop(); if(l && l instanceof Command){ l.f(); continue; } r = rs.pop(); // direct unity or anyvar if(l === r || l === _ || r === _){ continue; } // process variables (variables have priority) if(l instanceof Var){ if(l.unify(r, ls, rs, env)) continue; return null; } if(r instanceof Var){ if(r.unify(l, ls, rs, env)) continue; return null; } // invoke custom unifiers if(l instanceof Unifier){ if(l.unify(r, ls, rs, env)) continue; return null; } if(r instanceof Unifier){ if(r.unify(l, ls, rs, env)) continue; return null; } // check rough types if(typeof l != typeof r){ return null; } // special case: NaN if(typeof l == "number" && isNaN(l) && isNaN(r)){ continue; } // cut off impossible combinations if(typeof l != "object" && typeof l != "function" || !l || !r){ return null; } // process registered constructors var registry = unify.registry; for(var i = 0, len = registry.length; i < len; i += 2){ if(l instanceof registry[i] || r instanceof registry[i]){ if(registry[i + 1](l, r, ls, rs, env)) continue main; return null; } } // process registered filters registry = unify.filters; for(i = 0, len = registry.length; i < len; i += 2){ if(registry[i](l, r)){ if(registry[i + 1](l, r, ls, rs, env)) continue main; return null; } } // process naked objects if(!unifyObjects(l, objectType, null, r, objectType, null, ls, rs, env)) return null; } return env; } // exports unify._ = unify.any = _; unify.registry = registry; unify.filters = filters; unify.Env = Env; unify.Unifier = Unifier; unify.Variable = Var; unify.variable = variable; unify.open = open; unify.soft = soft; unify.isUnifier = isUnifier; unify.isVariable = isVariable; unify.isWrapped = isWrapped; unify.isOpen = isOpen; unify.isSoft = isSoft; return unify; }); unify-0.2.7/package-lock.json000066400000000000000000000104431330464455600161020ustar00rootroot00000000000000{ "name": "heya-unify", "version": "0.2.7", "lockfileVersion": 1, "requires": true, "dependencies": { "balanced-match": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=", "dev": true }, "brace-expansion": { "version": "1.1.11", "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", "dev": true, "requires": { "balanced-match": "1.0.0", "concat-map": "0.0.1" } }, "concat-map": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=", "dev": true }, "fs.realpath": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=", "dev": true }, "glob": { "version": "7.1.2", "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.2.tgz", "integrity": "sha512-MJTUg1kjuLeQCJ+ccE4Vpa6kKVXkPYJ2mOCQyUuKLcLQsdrMCpBPUi8qVE6+YuaJkozeA9NusTAw3hLr8Xe5EQ==", "dev": true, "requires": { "fs.realpath": "1.0.0", "inflight": "1.0.6", "inherits": "2.0.3", "minimatch": "3.0.4", "once": "1.4.0", "path-is-absolute": "1.0.1" } }, "heya-globalize": { "version": "1.2.1", "resolved": "https://registry.npmjs.org/heya-globalize/-/heya-globalize-1.2.1.tgz", "integrity": "sha512-lnXF5Vu0je61PaeWrlUDMaBI5p8GynmIza/qg81IN5L1xHMaAqgPGBdrHzU44m+F5IVF1yAK4PCBtMqDAi5ojw==", "dev": true, "requires": { "glob": "7.1.2", "minimist": "1.2.0", "mkdirp": "0.5.1" } }, "heya-ice": { "version": "0.1.11", "resolved": "https://registry.npmjs.org/heya-ice/-/heya-ice-0.1.11.tgz", "integrity": "sha1-XW2lnGC1nHAjqDRw+26XcddwWEk=" }, "inflight": { "version": "1.0.6", "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz", "integrity": "sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=", "dev": true, "requires": { "once": "1.4.0", "wrappy": "1.0.2" } }, "inherits": { "version": "2.0.3", "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz", "integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4=", "dev": true }, "minimatch": { "version": "3.0.4", "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", "dev": true, "requires": { "brace-expansion": "1.1.11" } }, "minimist": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz", "integrity": "sha1-o1AIsg9BOD7sH7kU9M1d95omQoQ=", "dev": true }, "mkdirp": { "version": "0.5.1", "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz", "integrity": "sha1-MAV0OOrGz3+MR2fzhkjWaX11yQM=", "dev": true, "requires": { "minimist": "0.0.8" }, "dependencies": { "minimist": { "version": "0.0.8", "resolved": "https://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz", "integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0=", "dev": true } } }, "once": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", "integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=", "dev": true, "requires": { "wrappy": "1.0.2" } }, "path-is-absolute": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=", "dev": true }, "wrappy": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=", "dev": true } } } unify-0.2.7/package.json000066400000000000000000000017431330464455600151570ustar00rootroot00000000000000{ "name": "heya-unify", "version": "0.2.7", "description": "Unify: a unification tool with a deep equivalence and partitioning of objects.", "main": "main.js", "directories": { "test": "tests" }, "dependencies": { "heya-ice": "^0.1.11" }, "devDependencies": { "heya-globalize": "^1.0.2" }, "scripts": { "test": "node tests/tests.js", "dist": "node node_modules/heya-globalize/index.js" }, "repository": { "type": "git", "url": "git://github.com/heya/unify.git" }, "keywords": [ "unification", "equivalence", "comparison", "deep equivalence", "code generation", "code construction" ], "author": "Eugene Lazutkin (http://lazutkin.com/)", "license": "BSD-3-Clause", "volo": { "type": "directory", "dependencies": { "heya-ice": "github:heya/ice" } }, "browserGlobals": { "!root": "heya.unify", "./main": "heya.unify", "heya-ice": "heya.ice" } } unify-0.2.7/tests/000077500000000000000000000000001330464455600140265ustar00rootroot00000000000000unify-0.2.7/tests/phantom.js000066400000000000000000000021161330464455600160320ustar00rootroot00000000000000phantom.onError = function(msg, trace){ var msgStack = ["PHANTOM ERROR: " + msg]; if(trace){ msgStack.push("TRACE:"); trace.forEach(function(t){ msgStack.push(" -> " + (t.file || t.sourceURL) + ": " + t.line + (t.function ? " (in function " + t.function + ")" : "")); }); } console.error(msgStack.join('\n')); phantom.exit(1); }; var page = require("webpage").create(); page.onError = function(msg){ console.error("ERROR: " + msg); phantom.exit(1); }; page.onAlert = function(msg){ console.log("ALERT: " + msg); }; page.onConsoleMessage = function(msg){ console.log(msg); }; page.onCallback = function(msg){ switch(msg){ case "success": phantom.exit(0); break; case "failure": phantom.exit(1); break; } } var scriptPath = require("system").args[0], path = require("fs").absolute( (scriptPath.length && scriptPath.charAt(0) == "/" ? "" : "./") + scriptPath).split("/"); path.pop(); path.push("tests.html"); page.open(path.join("/"), function(status){ if(status !== "success"){ console.error("ERROR: Can't load a web page."); phantom.exit(1); } }); unify-0.2.7/tests/tests.html000066400000000000000000000013651330464455600160630ustar00rootroot00000000000000 heya-unify test runner unify-0.2.7/tests/tests.js000066400000000000000000000476071330464455600155440ustar00rootroot00000000000000/* UMD.define */ (typeof define=="function"&&define||function(d,f,m){m={module:module,require:require};module.exports=f.apply(null,d.map(function(n){return m[n]||require(n)}))}) (["module", "heya-ice", "../main", "../utils/preprocess", "../unifiers/matchString", "../unifiers/matchTypeOf", "../unifiers/matchInstanceOf", "../unifiers/match", "../unifiers/ref", "../utils/walk", "../utils/clone", "../utils/assemble", "../utils/deref", "../utils/replace"], function(module, ice, unify, preprocess, matchString, matchTypeOf, matchInstanceOf, match, ref, walk, clone, assemble, deref, replace){ "use strict"; ice = ice.specialize(module); // test harness function out(msg){ console.log(msg); }; var _total = 0, _errors = 0, _current = null, _local = 0; function res(msg, isError){ ++_local; ++_total; if(isError){ ++_errors; console.log(msg); } }; var SHOW_FAILED_TEST_CODE = true; function submit(msg, success){ if(success){ res("Success: " + msg + " --- in " + _current + ", #" + (_local + 1)); }else{ res("Failed: " + msg + " --- in " + _current + ", #" + (_local + 1), true); } } function TEST(condition){ return "submit('" + quoteString(condition) + "', (" + condition + "))"; } function quoteString(text){ return text.replace(/['"\\]/g, "\\$&"); } // setup var _ = unify._, v = unify.variable, open = unify.open, soft = unify.soft, isOpen = unify.isOpen, isSoft = unify.isSoft; // tests var tests = [ function test_constants(){ eval(TEST("unify(1, 1)")); eval(TEST("unify(0, 0)")); eval(TEST("unify(null, null)")); eval(TEST("unify(undefined, undefined)")); eval(TEST("unify(true, true)")); eval(TEST("unify(false, false)")); eval(TEST("unify('', '')")); eval(TEST("unify('1', '1')")); eval(TEST("unify(Infinity, Infinity)")); eval(TEST("unify(-Infinity, -Infinity)")); eval(TEST("unify(NaN, NaN)")); eval(TEST("!unify(1, 2)")); eval(TEST("!unify(1, true)")); eval(TEST("!unify(1, '1')")); eval(TEST("!unify(1, [])")); eval(TEST("!unify(1, {})")); }, function test_anyvar(){ eval(TEST("unify(_, 1)")); eval(TEST("unify(_, 2)")); eval(TEST("unify(_, true)")); eval(TEST("unify(_, '1')")); eval(TEST("unify(_, [])")); eval(TEST("unify(_, {})")); eval(TEST("unify(1, _)")); eval(TEST("unify(2, _)")); eval(TEST("unify(true, _)")); eval(TEST("unify('1', _)")); eval(TEST("unify([], _)")); eval(TEST("unify({}, _)")); }, function test_exact_arrays(){ eval(TEST("unify([], [])")); eval(TEST("unify([1], [1])")); eval(TEST("unify([1,2], [1,2])")); eval(TEST("!unify([], [1])")); eval(TEST("!unify([1], [2])")); eval(TEST("!unify([2,1], [1,2])")); eval(TEST("unify([1,_,3], [_,2,_])")); eval(TEST("unify([_,_,3], [1,_,_])")); eval(TEST("unify([[]], [[]])")); eval(TEST("unify([[], []], [[], []])")); }, function test_exact_objects(){ eval(TEST("unify({}, {})")); eval(TEST("unify({a: 1}, {a: 1})")); eval(TEST("unify({a: 1, b: 2}, {b: 2, a: 1})")); eval(TEST("!unify({}, {a: 1})")); eval(TEST("!unify({a: 1}, {a: 2})")); eval(TEST("!unify({a: 1}, {b: 1})")); eval(TEST("unify({a: _, b: 2}, {a: 1, b: _})")); eval(TEST("unify({a: {a: 1}, b: 2}, {a: {a: 1}, b: 2})")); eval(TEST("!unify({a: {a: 1}, b: 2}, {a: {a: 3}, b: 2})")); eval(TEST("!unify({a: {a: 1}, b: 2}, {a: {a: 1}, b: 3})")); }, function test_variables(){ var result = unify(1, 1); eval(TEST("result && unify(result.values, {})")); result = unify(1, _); eval(TEST("result && unify(result.values, {})")); result = unify(1, v("x")); eval(TEST("result")); eval(TEST("unify(result.values, {x: 1})")); eval(TEST("v('x').bound(result)")); eval(TEST("v('x').get(result) === 1")); result = unify(v("y"), v("x")); eval(TEST("result")); eval(TEST("unify(result.values, {})")); eval(TEST("unify(result.variables, {x: {x: 1, y: 1}, y: {x: 1, y: 1}})")); eval(TEST("!v('x').bound(result)")); eval(TEST("!v('y').bound(result)")); eval(TEST("v('x').alias('y', result)")); eval(TEST("v('y').alias('x', result)")); eval(TEST("!v('x').alias('z', result)")); eval(TEST("!v('y').alias('z', result)")); result = unify(v("y"), _); eval(TEST("result && unify(result.values, {})")); result = unify([1, v("x")], [v("y"), 2]); eval(TEST("result")); eval(TEST("unify(result.values, {x: 2, y: 1})")); eval(TEST("v('x').bound(result)")); eval(TEST("v('x').get(result) === 2")); eval(TEST("v('y').bound(result)")); eval(TEST("v('y').get(result) === 1")); result = unify({a: 1, b: v("x")}, {a: v("y"), b: 2}); eval(TEST("result")); eval(TEST("unify(result.values, {x: 2, y: 1})")); eval(TEST("v('x').bound(result)")); eval(TEST("v('x').get(result) === 2")); eval(TEST("v('y').bound(result)")); eval(TEST("v('y').get(result) === 1")); result = unify({a: 1, b: v("x")}, {a: v("y"), c: 2}); eval(TEST("!result")); result = unify({c: 1, b: v("x")}, {a: v("y"), b: 2}); eval(TEST("!result")); }, function test_regexes(){ eval(TEST("unify(/\\b\\w+\\b/, /\\b\\w+\\b/)")); eval(TEST("!unify(/\\b\\w+\\b/m, /\\b\\w+\\b/)")); eval(TEST("!unify(/\\b\\w+\\b/m, /\\b\\w+\\b/g)")); eval(TEST("!unify(/\\b\\w+\\b/, /\\b\\w+\\b/i)")); eval(TEST("!unify(/\\b\\w+\\b/, 1)")); eval(TEST("unify(/\\b\\w+\\b/, new RegExp('\\\\b\\\\w+\\\\b'))")); }, function test_dates(){ eval(TEST("unify(new Date(2013, 6, 4), new Date(2013, 6, 4))")); eval(TEST("!unify(new Date(2013, 6, 4), new Date(2012, 6, 4))")); eval(TEST("!unify(new Date(2013, 6, 4), new Date(2013, 6, 4, 6))")); eval(TEST("unify(new Date(2013, 6, 4, 6), new Date(2013, 6, 4, 6))")); }, function test_open_structures(){ eval(TEST("unify({a: 1, b: 2, c: 3}, open({a: 1}))")); eval(TEST("unify(open({a: 1}), {a: 1, b: 2, c: 3})")); eval(TEST("unify([1, 2, 3], open([1,2]))")); eval(TEST("unify(open([1, 2]), [1, 2, 3])")); eval(TEST("unify(open({a: 1}), open({b: 2}))")); eval(TEST("unify(open([1]), open([1, 2]))")); }, function test_soft_structures(){ var x = v("x"); var result = unify([soft({a: 1}), soft({b: 2})], soft([x, x])); eval(TEST("result")); eval(TEST("isSoft(x.get(result))")); eval(TEST("x.get(result).type === 'soft'")); eval(TEST("unify(x.get(result).object, {a: 1, b: 2})")); result = unify([soft({a: 1}), x], soft([x, soft({b: 2})])); eval(TEST("result")); eval(TEST("isSoft(x.get(result))")); eval(TEST("x.get(result).type === 'soft'")); eval(TEST("unify(x.get(result).object, {a: 1, b: 2})")); }, function test_soft_presets(){ var x = v("x"), env = unify(x, soft({})); var result = unify([1], [x], env); eval(TEST("!result")); result = unify([open({a: 1}), open({b: 2})], [x, x], env); eval(TEST("result")); eval(TEST("isSoft(x.get(result))")); eval(TEST("x.get(result).type === 'soft'")); eval(TEST("unify(x.get(env).object, {a: 1, b: 2})")); }, function test_complex_structures(){ var x = v("x"), y = v("y"); var tree = { value: 0, left: { value: 1, left: { value: 3 }, right: { value: 4 } }, right: { value: 2, left: null, right: { value: 3 } } }; var result = unify(tree, { value: x, left: open({left: y}), right: open({right: y}) }); eval(TEST("result")); eval(TEST("x.get(result) === 0")); eval(TEST("unify(y.get(result), {value: 3})")); }, function test_preprocess(){ var l = { x: 5, y: { a: 42, b: {}, c: [1, 2, 3] }, z: "ah!" }, r = { y: { b: {} }, z: "ah!" }; var result = unify(l, r); eval(TEST("!result")); result = unify(l, preprocess(r)); eval(TEST("!result")); result = unify(l, preprocess(r, true)); eval(TEST("result")); result = unify(l.y, {c: [1, 2]}); eval(TEST("!result")); result = unify(l.y, preprocess({c: [1, 2]})); eval(TEST("!result")); result = unify(l.y, preprocess({c: [1, 2]}, false, true)); eval(TEST("!result")); result = unify(l.y, preprocess({c: [1, 2]}, true, true)); eval(TEST("result")); }, function test_matchString(){ var result = unify("12345", matchString(/1(2)3/)); eval(TEST("result")); result = unify("12345", matchString(/1(2)3/, null, {input: "12345", index: 0})); eval(TEST("result")); result = unify("12345", matchString(/1(2)3/, ["123", "2"])); eval(TEST("result")); // var x = v("x"), y = v("y"); result = unify("12345", matchString(/1(2)3/, x, y)); eval(TEST("result")); eval(TEST("unify(x.get(result), ['123', '2'])")); eval(TEST("unify(y.get(result), {index: 0, input: '12345'})")); eval(TEST("unify(y.get(result), open({index: 0}))")); // result = unify("12345", matchString(/1(2)3/, [_, x], open({index: y}))); eval(TEST("result")); eval(TEST("x.get(result) === '2'")); eval(TEST("y.get(result) === 0")); }, function test_matchTypeOf(){ var result = unify(1, matchTypeOf("number")); eval(TEST("result")); result = unify("a", matchTypeOf("string")); eval(TEST("result")); result = unify(true, matchTypeOf("boolean")); eval(TEST("result")); result = unify(undefined, matchTypeOf("undefined")); eval(TEST("result")); result = unify(null, matchTypeOf("object")); eval(TEST("result")); result = unify([], matchTypeOf("object")); eval(TEST("result")); result = unify({}, matchTypeOf("object")); eval(TEST("result")); result = unify(function(){}, matchTypeOf("function")); eval(TEST("result")); result = unify("a", matchTypeOf(["number", "string", "boolean"])); eval(TEST("result")); result = unify(null, matchTypeOf(["function", "object"])); eval(TEST("result")); result = unify(unify, matchTypeOf(["function", "object"])); eval(TEST("result")); result = unify([], matchTypeOf(["number", "string", "boolean"])); eval(TEST("!result")); result = unify(1, matchTypeOf(["function", "object"])); eval(TEST("!result")); }, function test_matchInstanceOf(){ function A(){} function B(){} B.prototype = Object.create(A.prototype); function C(){} C.prototype = Object.create(B.prototype); function D(){} function E(){} E.prototype = Object.create(D.prototype); var result = unify(new A, matchInstanceOf(Object)); eval(TEST("result")); result = unify(new A, matchInstanceOf(A)); eval(TEST("result")); result = unify(new A, matchInstanceOf(B)); eval(TEST("!result")); result = unify(new A, matchInstanceOf(C)); eval(TEST("!result")); result = unify(new A, matchInstanceOf(D)); eval(TEST("!result")); result = unify(new A, matchInstanceOf(E)); eval(TEST("!result")); result = unify(new B, matchInstanceOf(Object)); eval(TEST("result")); result = unify(new B, matchInstanceOf(A)); eval(TEST("result")); result = unify(new B, matchInstanceOf(B)); eval(TEST("result")); result = unify(new B, matchInstanceOf(C)); eval(TEST("!result")); result = unify(new B, matchInstanceOf(D)); eval(TEST("!result")); result = unify(new B, matchInstanceOf(E)); eval(TEST("!result")); result = unify(new C, matchInstanceOf(Object)); eval(TEST("result")); result = unify(new C, matchInstanceOf(A)); eval(TEST("result")); result = unify(new C, matchInstanceOf(B)); eval(TEST("result")); result = unify(new C, matchInstanceOf(C)); eval(TEST("result")); result = unify(new C, matchInstanceOf(D)); eval(TEST("!result")); result = unify(new C, matchInstanceOf(E)); eval(TEST("!result")); result = unify(new D, matchInstanceOf(Object)); eval(TEST("result")); result = unify(new D, matchInstanceOf(A)); eval(TEST("!result")); result = unify(new D, matchInstanceOf(B)); eval(TEST("!result")); result = unify(new D, matchInstanceOf(C)); eval(TEST("!result")); result = unify(new D, matchInstanceOf(D)); eval(TEST("result")); result = unify(new D, matchInstanceOf(E)); eval(TEST("!result")); result = unify(new E, matchInstanceOf(Object)); eval(TEST("result")); result = unify(new E, matchInstanceOf(A)); eval(TEST("!result")); result = unify(new E, matchInstanceOf(B)); eval(TEST("!result")); result = unify(new E, matchInstanceOf(C)); eval(TEST("!result")); result = unify(new E, matchInstanceOf(D)); eval(TEST("result")); result = unify(new E, matchInstanceOf(E)); eval(TEST("result")); result = unify(new Date, matchInstanceOf(Object)); eval(TEST("result")); result = unify(new Date, matchInstanceOf(Date)); eval(TEST("result")); result = unify(new Date, matchInstanceOf(Array)); eval(TEST("!result")); result = unify([], matchInstanceOf(Object)); eval(TEST("result")); result = unify([], matchInstanceOf(Date)); eval(TEST("!result")); result = unify([], matchInstanceOf(Array)); eval(TEST("result")); result = unify({}, matchInstanceOf(Object)); eval(TEST("result")); result = unify({}, matchInstanceOf(Date)); eval(TEST("!result")); result = unify({}, matchInstanceOf(Array)); eval(TEST("!result")); }, function test_match(){ var smallNumber = match(function(val){ return typeof val == "number" && 0 < val && val < 10; }); var result = unify(5, smallNumber); eval(TEST("result")); var result = unify(10, smallNumber); eval(TEST("!result")); var result = unify(0, smallNumber); eval(TEST("!result")); var result = unify("5", smallNumber); eval(TEST("!result")); }, function test_walk(){ var result = {}; walk({ a: [1, true, [0, NaN, Infinity, Math.sin]], b: ["hello!", new Date, /\d+/, {g: undefined}], c: null, d: { e: [], f: {} } }, { processOther: function(s){ var t = typeof s; if(typeof result[t] != "number"){ result[t] = 0; } ++result[t]; } }); var expected = { 'boolean': 1, 'number': 4, 'string': 1, 'function': 1, 'object': 1, 'undefined': 1 }; eval(TEST("unify(result, expected)")); }, function test_clone(){ var source = { a: [1, true, [0, NaN, Infinity, Math.sin]], b: ["hello!", new Date, /\d+/, {g: undefined}], c: null, d: { e: [], f: {} } }; var result = clone(source); eval(TEST("result !== source")); eval(TEST("unify(result, source)")); var left = v("left"), right = v("right"); var env = unify({left: left, right: right}, { left: {left: 1, right: 2}, right: {left: 8, right: 9} }); result = clone({ left: { left: left, right: {left: 3, right: 4} }, right: { left: {left: 6, right: 7}, right: right } }, env); var expected = { left: { left: {left: 1, right: 2}, right: {left: 3, right: 4} }, right: { left: {left: 6, right: 7}, right: {left: 8, right: 9} } }; eval(TEST("unify(result, expected)")); }, function test_assemble(){ var source = { a: [1,,null], b: {c: "hey"} }; var result = assemble(source); eval(TEST("result === source")); eval(TEST("unify(result, source)")); source = {x: [{y: false}]}; var env = unify(v("x"), source); eval(TEST("v('x').bound(env)")); eval(TEST("v('x').get(env) === source")); eval(TEST("unify(v('x').get(env), source)")); source = {z: v("x")}; result = assemble(source, env); eval(TEST("result !== source")); eval(TEST("unify(result, source, env)")); eval(TEST("unify(result, {z: {x: [{y: false}]}})")); }, function test_deref(){ var source = { a: [1,,null], b: {c: "hey"} }; var result = deref(source); eval(TEST("result === source")); eval(TEST("unify(result, source)")); source = {x: [{y: false}]}; var env = unify(v("x"), source); eval(TEST("v('x').bound(env)")); eval(TEST("v('x').get(env) === source")); eval(TEST("unify(v('x').get(env), source)")); source = {z: v("x")}; result = deref(source, env); eval(TEST("result === source")); eval(TEST("unify(result, source, env)")); eval(TEST("unify(result, {z: {x: [{y: false}]}})")); }, function test_ref(){ var source = { left: {left: 1, right: 2}, right: {left: 3, right: 4} }, pattern = { left: ref(v("lnode"), {left: 1, right: v("right")}), right: ref("rnode", {left: v("left"), right: 4}) }; var env = unify(pattern, source); eval(TEST("env")); eval(TEST("v('left').bound(env)")); eval(TEST("v('left').get(env) === 3")); eval(TEST("v('right').bound(env)")); eval(TEST("v('right').get(env) === 2")); eval(TEST("v('lnode').bound(env)")); eval(TEST("unify(v('lnode'), {left: 1, right: 2}, env)")); eval(TEST("v('rnode').bound(env)")); eval(TEST("unify(v('rnode'), {left: 3, right: 4}, env)")); }, function test_filters(){ var counter = 0; function Foo(name){ this.counter = ++counter; this.name = name; this.flag = true; } var l = {x: new Foo("Sam"), y: new Foo("Mary")}, r = {x: new Foo("Sam"), y: new Foo("Mary")}; eval(TEST("counter === 4")); // delayed filter unify.filters.push( function test(l, r){ return l.flag || r.flag; }, function unify(l, r, ls, rs, env){ if(!l.flag || !r.flag){ return false; } ls.push(l.name); rs.push(r.name); return true; } ); eval(TEST("unify(l, r)")); unify.filters.pop(); unify.filters.pop(); // immediate filter unify.filters.push( function test(l, r){ return l.flag || r.flag; }, function unify(l, r, ls, rs, env){ if(!l.flag || !r.flag){ return false; } return l.name === r.name; } ); eval(TEST("unify(l, r)")); unify.filters.pop(); unify.filters.pop(); // no custom filters eval(TEST("!unify(l, r)")); // instanceof-based custom unifier unify.registry.push( Foo, function unify(l, r, ls, rs, env){ if(typeof r == "string"){ ls.push(l.name); rs.push(r); return true; } if(!r.flag){ return false; } ls.push(l.name); rs.push(r.name); return true; } ); eval(TEST("unify(l, r)")); unify.registry.pop(); unify.registry.pop(); }, function test_replace(){ var x = v("x"), y = v("y"), val = v("val"), env = unify({ val: 3, pos: [1, 2] }, { val: val, pos: [x, y] }); eval(TEST("env")); eval(TEST("x.bound(env)")); eval(TEST("unify(x, 1, env)")); eval(TEST("y.bound(env)")); eval(TEST("unify(y, 2, env)")); eval(TEST("val.bound(env)")); eval(TEST("unify(val, 3, env)")); eval(TEST("replace('${x} + ${y} = ${val}', env) === '1 + 2 = 3'")); }, function test_nullProto(){ var x = Object.create(null), y = Object.create(null), z = Object.create(null); x.a = 1, x.b = [], x.c = null; y.a = 1, y.b = [], y.c = null; z.a = 2, z.b = [], z.c = null; var env = unify(x, y); eval(TEST("env")); env = unify(x, {c: null, b: [], a: 1}); eval(TEST("env")); env = unify(y, {c: null, b: [], a: 1}); eval(TEST("env")); env = unify(x, z); eval(TEST("!env")); env = unify(x, {c: null, b: [], a: 2}); eval(TEST("!env")); } ]; function runTests(){ _total = _errors = 0; var exceptionFlag = false; out("Starting tests..."); for(var i = 0, l = tests.length; i < l; ++i){ _current = tests[i].name; _local = 0; try{ tests[i](); }catch(e){ exceptionFlag = true; if(typeof console != "undefined"){ // IE < 9 :-( console.log("Unhandled exception in test #" + i + " (" + tests[i].name + "): " + e.message); if(e.stack){ console.log("Stack: ", e.stack); } if(SHOW_FAILED_TEST_CODE){ console.log("Code: ", tests[i].toString()); } } } } out(_errors ? "Failed " + _errors + " out of " + _total + " tests." : "Finished " + _total + " tests."); if(typeof process != "undefined"){ process.exit(_errors || exceptionFlag ? 1 : 0); }else if(typeof window != "undefined" && window){ if(typeof window.callPhantom != "undefined"){ window.callPhantom(_errors || exceptionFlag ? "failure" : "success"); } } } runTests(); }); unify-0.2.7/unifiers/000077500000000000000000000000001330464455600145105ustar00rootroot00000000000000unify-0.2.7/unifiers/match.js000066400000000000000000000007321330464455600161440ustar00rootroot00000000000000/* UMD.define */ (typeof define=="function"&&define||function(d,f,m){m={module:module,require:require};module.exports=f.apply(null,d.map(function(n){return m[n]||require(n)}))}) (["../main"], function(unify){ "use strict"; function Match(f){ this.f = f; } Match.prototype = Object.create(unify.Unifier.prototype); Match.prototype.unify = function(val, ls, rs, env){ return this.f(val, ls, rs, env); }; return function match(f){ return new Match(f); }; }); unify-0.2.7/unifiers/matchInstanceOf.js000066400000000000000000000012121330464455600201100ustar00rootroot00000000000000/* UMD.define */ (typeof define=="function"&&define||function(d,f,m){m={module:module,require:require};module.exports=f.apply(null,d.map(function(n){return m[n]||require(n)}))}) (["../main"], function(unify){ "use strict"; function MatchInstanceOf(types){ this.types = types instanceof Array ? types : [types]; } MatchInstanceOf.prototype = Object.create(unify.Unifier.prototype); MatchInstanceOf.prototype.unify = function(val, ls, rs){ return val && !unify.isVariable(val) && this.types.some(function(type){ return val instanceof type; }); }; return function matchInstanceOf(types){ return new MatchInstanceOf(types); }; }); unify-0.2.7/unifiers/matchString.js000066400000000000000000000017131330464455600173330ustar00rootroot00000000000000/* UMD.define */ (typeof define=="function"&&define||function(d,f,m){m={module:module,require:require};module.exports=f.apply(null,d.map(function(n){return m[n]||require(n)}))}) (["../main"], function(unify){ "use strict"; function MatchString(regexp, matches, props){ this.regexp = regexp; this.matches = matches; this.props = props; } MatchString.prototype = Object.create(unify.Unifier.prototype); MatchString.prototype.unify = function(val, ls, rs){ if(unify.isVariable(val)){ // cannot match with an unbound variable return false; } var result = this.regexp.exec("" + val); if(result){ if(this.matches){ ls.push(this.matches); rs.push(Array.prototype.slice.call(result, 0)); } if(this.props){ ls.push(this.props); rs.push({index: result.index, input: result.input}); } } return result; }; return function matchString(regexp, matches, props){ return new MatchString(regexp, matches, props); }; }); unify-0.2.7/unifiers/matchTypeOf.js000066400000000000000000000011171330464455600172710ustar00rootroot00000000000000/* UMD.define */ (typeof define=="function"&&define||function(d,f,m){m={module:module,require:require};module.exports=f.apply(null,d.map(function(n){return m[n]||require(n)}))}) (["../main"], function(unify){ "use strict"; function MatchTypeOf(types){ this.types = types instanceof Array ? types : [types]; } MatchTypeOf.prototype = Object.create(unify.Unifier.prototype); MatchTypeOf.prototype.unify = function(val, ls, rs){ return !unify.isVariable(val) && this.types.indexOf(typeof val) >= 0; }; return function matchTypeOf(types){ return new MatchTypeOf(types); }; }); unify-0.2.7/unifiers/ref.js000066400000000000000000000013601330464455600156220ustar00rootroot00000000000000/* UMD.define */ (typeof define=="function"&&define||function(d,f,m){m={module:module,require:require};module.exports=f.apply(null,d.map(function(n){return m[n]||require(n)}))}) (["../main"], function(unify){ "use strict"; var Var = unify.Variable; function Ref(variable, value){ this.variable = typeof variable == "string" ? new Var(variable) : variable; this.value = value; Var.call(this, this.variable.name); } Ref.prototype = Object.create(Var.prototype); Ref.prototype.declaredClass = "unify/Ref"; Ref.prototype.unify = function(val, ls, rs, env){ ls.push(this.value, this.variable); rs.push(val, val); return true; }; function ref(variable, value){ return new Ref(variable, value); } ref.Ref = Ref; return ref; }); unify-0.2.7/utils/000077500000000000000000000000001330464455600140245ustar00rootroot00000000000000unify-0.2.7/utils/assemble.js000066400000000000000000000060401330464455600161550ustar00rootroot00000000000000/* UMD.define */ (typeof define=="function"&&define||function(d,f,m){m={module:module,require:require};module.exports=f.apply(null,d.map(function(n){return m[n]||require(n)}))}) (["module", "heya-ice/assert", "../main", "./walk"], function(module, ice, unify, walk){ "use strict"; ice = ice.specialize(module); var empty = {}; function postArray(context){ var stackOut = context.stackOut, s = this.s, l = s.length, i = 0, j = stackOut.length - 1, t; main: { for(; i < l; ++i){ if(s.hasOwnProperty(i)){ t = stackOut[j--]; if(typeof t == "number" && isNaN(t) ? typeof s[i] == "number" && !isNaN(s[i]) : s[i] !== t){ break main; } } } l = stackOut.length - 1 - j; if(l){ stackOut.splice(-l, l, s); }else{ stackOut.push(s); } return; } t = []; for(i = 0; i < l; ++i){ if(s.hasOwnProperty(i)){ t[i] = stackOut.pop(); } } stackOut.push(t); } function postObject(context){ var stackOut = context.stackOut, s = this.s, k, j = stackOut.length - 1, t; main: { for(k in s){ if(s.hasOwnProperty(k)){ t = stackOut[j--]; if(typeof t == "number" && isNaN(t) ? typeof s[k] == "number" && !isNaN(s[k]) : s[k] !== t){ break main; } } } var l = stackOut.length - 1 - j; if(l){ stackOut.splice(-l, l, s); }else{ stackOut.push(s); } return; } t = {}; for(k in s){ if(s.hasOwnProperty(k)){ t[k] = stackOut.pop(); } } stackOut.push(t); } function processObject(val, context){ if(val === unify._){ context.stackOut.push(val); }else{ var stack = context.stack; stack.push(new (walk.Command)(postObject, val)); for(var k in val){ if(val.hasOwnProperty(k)){ stack.push(val[k]); } } } } function processOther(val, context){ context.stackOut.push(val); } var registry = [ walk.Command, function processCommand(val, context){ val.f(context); }, Array, function processArray(val, context){ var stack = context.stack; stack.push(new (walk.Command)(postArray, val)); for(var i = 0, l = val.length; i < l; ++i){ if(val.hasOwnProperty(i)){ stack.push(val[i]); } } }, unify.Variable, function processVariable(val, context){ var env = context.env; if(val.bound(env)){ context.stack.push(val.get(env)); }else{ context.stackOut.push(val); } }, unify.Unifier, processOther, Date, processOther, RegExp, processOther ], filters = []; function assemble(source, env, opt){ opt = opt || empty; var context = opt.context || {}, stackOut = []; context.stackOut = stackOut; context.env = env; walk(source, { processObject: opt.processObject || processObject, processOther: opt.processOther || processOther, registry: opt.registry || assemble.registry, filters: opt.filters || assemble.filters, context: context }); ice.assert(stackOut.length == 1); return stackOut[0]; } assemble.registry = registry; assemble.filters = filters; return assemble; }); unify-0.2.7/utils/clone.js000066400000000000000000000051701330464455600154650ustar00rootroot00000000000000/* UMD.define */ (typeof define=="function"&&define||function(d,f,m){m={module:module,require:require};module.exports=f.apply(null,d.map(function(n){return m[n]||require(n)}))}) (["module", "heya-ice/assert", "../main", "./walk"], function(module, ice, unify, walk){ "use strict"; ice = ice.specialize(module); var empty = {}; function postArray(context){ var stackOut = context.stackOut, t = []; for(var i = 0, s = this.s, l = s.length; i < l; ++i){ if(s.hasOwnProperty(i)){ t[i] = stackOut.pop(); } } stackOut.push(t); } function postObject(context){ var stackOut = context.stackOut, t = {}, s = this.s; for(var k in s){ if(s.hasOwnProperty(k)){ t[k] = stackOut.pop(); } } stackOut.push(t); } function processObject(val, context){ if(val === unify._){ context.stackOut.push(s); }else{ var stack = context.stack; stack.push(new (walk.Command)(postObject, val)); for(var k in val){ if(val.hasOwnProperty(k)){ stack.push(val[k]); } } } } var registry = [ walk.Command, function processCommand(val, context){ val.f(context); }, Array, function processArray(val, context){ var stack = context.stack; stack.push(new (walk.Command)(postArray, val)); for(var i = 0, l = val.length; i < l; ++i){ if(val.hasOwnProperty(i)){ stack.push(val[i]); } } }, unify.Variable, function processVariable(val, context){ var env = context.env; if(val.bound(env)){ context.stack.push(val.get(env)); }else{ context.stackOut.push(val); } }, unify.Unifier, function processAsValue(val, context){ context.stackOut.push(val); }, Date, function processDate(val, context){ context.stackOut.push(new Date(val.getTime())); }, RegExp, function processRegExp(val, context){ context.stackOut.push(new RegExp(val.source, (val.global ? "g" : "") + (val.multiline ? "m" : "") + (val.ignoreCase ? "i" : "") )); } ], filters = []; function processOther(val, context){ context.stackOut.push(val); } function clone(source, env, opt){ var stackOut = []; opt = opt || empty; var context = opt.context || {}, stackOut = []; context.stackOut = stackOut; context.env = env; walk(source, { processObject: opt.processObject || processObject, processOther: opt.processOther || processOther, registry: opt.registry || clone.registry, filters: opt.filters || clone.filters, context: context }); ice.assert(stackOut.length == 1); return stackOut[0]; } clone.registry = registry; clone.filters = filters; return clone; }); unify-0.2.7/utils/deref.js000066400000000000000000000047611330464455600154570ustar00rootroot00000000000000/* UMD.define */ (typeof define=="function"&&define||function(d,f,m){m={module:module,require:require};module.exports=f.apply(null,d.map(function(n){return m[n]||require(n)}))}) (["module", "heya-ice/assert", "../main", "./walk"], function(module, ice, unify, walk){ "use strict"; ice = ice.specialize(module); var empty = {}; function postArray(context){ var stackOut = context.stackOut, s = this.s, l = s.length, i = 0, j = stackOut.length - 1, t; for(; i < l; ++i){ if(s.hasOwnProperty(i)){ s[i] = stackOut[j--]; } } l = stackOut.length - 1 - j; if(l){ stackOut.splice(-l, l, s); }else{ stackOut.push(s); } } function postObject(context){ var stackOut = context.stackOut, s = this.s, k, j = stackOut.length - 1, t; for(k in s){ if(s.hasOwnProperty(k)){ s[k] = stackOut[j--]; } } var l = stackOut.length - 1 - j; if(l){ stackOut.splice(-l, l, s); }else{ stackOut.push(s); } } function processObject(val, context){ if(val === unify._){ context.stackOut.push(val); }else{ var stack = context.stack; stack.push(new (walk.Command)(postObject, val)); for(var k in val){ if(val.hasOwnProperty(k)){ stack.push(val[k]); } } } } function processOther(val, context){ context.stackOut.push(val); } var registry = [ walk.Command, function processCommand(val, context){ val.f(context); }, Array, function processArray(val, context){ var stack = context.stack; stack.push(new (walk.Command)(postArray, val)); for(var i = 0, l = val.length; i < l; ++i){ if(val.hasOwnProperty(i)){ stack.push(val[i]); } } }, unify.Variable, function processVariable(val, context){ var env = context.env; if(val.bound(env)){ context.stack.push(val.get(env)); }else{ context.stackOut.push(val); } }, unify.Unifier, processOther, Date, processOther, RegExp, processOther ], filters = []; function deref(source, env, opt){ opt = opt || empty; var context = opt.context || {}, stackOut = []; context.stackOut = stackOut; context.env = env; walk(source, { processObject: opt.processObject || processObject, processOther: opt.processOther || processOther, registry: opt.registry || deref.registry, filters: opt.filters || deref.filters, context: context }); ice.assert(stackOut.length == 1); return stackOut[0]; } deref.registry = registry; deref.filters = filters; return deref; }); unify-0.2.7/utils/preprocess.js000066400000000000000000000045741330464455600165610ustar00rootroot00000000000000/* UMD.define */ (typeof define=="function"&&define||function(d,f,m){m={module:module,require:require};module.exports=f.apply(null,d.map(function(n){return m[n]||require(n)}))}) (["module", "heya-ice/assert", "../main", "./walk"], function(module, ice, unify, walk){ "use strict"; ice = ice.specialize(module); var empty = {}; function postArray(context){ var stackOut = context.stackOut, wrapArray = context.wrapArray, t = []; for(var i = 0, s = this.s, l = s.length; i < l; ++i){ if(s.hasOwnProperty(i)){ t[i] = stackOut.pop(); } } stackOut.push(wrapArray ? wrapArray(t) : t); } function postObject(context){ var stackOut = context.stackOut, wrapObject = context.wrapObject, t = {}, s = this.s; for(var k in s){ if(s.hasOwnProperty(k)){ t[k] = stackOut.pop(); } } stackOut.push(wrapObject ? wrapObject(t) : t); } function processObject(val, context){ if(val === unify._){ context.stackOut.push(val); }else{ var stack = context.stack; stack.push(new (walk.Command)(postObject, val)); for(var k in val){ if(val.hasOwnProperty(k)){ stack.push(val[k]); } } } } function processOther(val, context){ context.stackOut.push(val); } var registry = [ walk.Command, function processCommand(val, context){ val.f(context); }, Array, function processArray(val, context){ var stack = context.stack; stack.push(new (walk.Command)(postArray, val)); for(var i = 0, l = val.length; i < l; ++i){ if(val.hasOwnProperty(i)){ stack.push(val[i]); } } }, unify.Variable, processOther, unify.Unifier, processOther, Date, processOther, RegExp, processOther ], filters = []; function preprocess(source, nonExactObjects, nonExactArrays, opt){ opt = opt || empty; var context = opt.context || {}, stackOut = []; context.stackOut = stackOut; context.wrapObject = nonExactObjects && unify.open; context.wrapArray = nonExactArrays && unify.open; walk(source, { processObject: opt.processObject || processObject, processOther: opt.processOther || processOther, registry: opt.registry || preprocess.registry, filters: opt.filters || preprocess.filters, context: context }); ice.assert(stackOut.length == 1); return stackOut[0]; } preprocess.registry = registry; preprocess.filters = filters; return preprocess; }); unify-0.2.7/utils/replace.js000066400000000000000000000006631330464455600160020ustar00rootroot00000000000000/* UMD.define */ (typeof define=="function"&&define||function(d,f,m){m={module:module,require:require};module.exports=f.apply(null,d.map(function(n){return m[n]||require(n)}))}) ([], function(){ "use strict"; return function replace(tmpl, env){ return tmpl.replace(/\$+\{([^\}\s\r\n]+)\}/g, function(match, name){ if(match.length - name.length > 3){ return match.substring(1); } return env.values[name]; }); }; }); unify-0.2.7/utils/walk.js000066400000000000000000000034011330464455600153160ustar00rootroot00000000000000/* UMD.define */ (typeof define=="function"&&define||function(d,f,m){m={module:module,require:require};module.exports=f.apply(null,d.map(function(n){return m[n]||require(n)}))}) ([], function(){ "use strict"; var empty = {}; function nop(){} function Command(f, s){ this.f = f; this.s = s; } function processCommand(val, context){ val.f(context); } function processObject(val, context){ var stack = context.stack; for(var k in val){ if(val.hasOwnProperty(k)){ stack.push(val[k]); } } } function processArray(val, context){ var stack = context.stack; for(var i = 0, l = val.length; i < l; ++i){ if(val.hasOwnProperty(i)){ stack.push(val[i]); } } } var defaultRegistry = [ Command, processCommand, Array, processArray, Date, nop, RegExp, nop ], defaultFilters = []; function walk(o, opt){ // non-recursive stack-based walk about an object tree opt = opt || empty; var doObject = opt.processObject || processObject, doOther = opt.processOther || nop, registry = opt.registry || defaultRegistry, filters = opt.filters || defaultFilters, context = opt.context || {}, stack = [o]; context.stack = stack; main: while(stack.length){ o = stack.pop(); if(o && typeof o == "object"){ // process registered constructors for(var i = 0; i < registry.length; i += 2){ if(o instanceof registry[i]){ registry[i + 1](o, context); continue main; } } // process registered filters for(i = 0; i < filters.length; ++i){ if(filters[i](o, context)){ continue main; } } // process naked objects doObject(o, context); continue; } doOther(o, context); } } walk.Command = Command; return walk; }); unify-0.2.7/yarn.lock000066400000000000000000000056451330464455600145210ustar00rootroot00000000000000# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. # yarn lockfile v1 balanced-match@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.0.tgz#89b4d199ab2bee49de164ea02b89ce462d71b767" brace-expansion@^1.1.7: version "1.1.11" resolved "https://registry.yarnpkg.com/brace-expansion/-/brace-expansion-1.1.11.tgz#3c7fcbf529d87226f3d2f52b966ff5271eb441dd" dependencies: balanced-match "^1.0.0" concat-map "0.0.1" concat-map@0.0.1: version "0.0.1" resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b" fs.realpath@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/fs.realpath/-/fs.realpath-1.0.0.tgz#1504ad2523158caa40db4a2787cb01411994ea4f" glob@^7.0.0: version "7.1.2" resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.2.tgz#c19c9df9a028702d678612384a6552404c636d15" dependencies: fs.realpath "^1.0.0" inflight "^1.0.4" inherits "2" minimatch "^3.0.4" once "^1.3.0" path-is-absolute "^1.0.0" heya-globalize@^1.0.2: version "1.2.1" resolved "https://registry.yarnpkg.com/heya-globalize/-/heya-globalize-1.2.1.tgz#03ed1c99c02c849a0de46a4246baf8f4d681eedd" dependencies: glob "^7.0.0" minimist "^1.2.0" mkdirp "^0.5.1" heya-ice@^0.1.11: version "0.1.11" resolved "https://registry.yarnpkg.com/heya-ice/-/heya-ice-0.1.11.tgz#5d6da59c60b59c7023a83470fb6e9771d7705849" inflight@^1.0.4: version "1.0.6" resolved "https://registry.yarnpkg.com/inflight/-/inflight-1.0.6.tgz#49bd6331d7d02d0c09bc910a1075ba8165b56df9" dependencies: once "^1.3.0" wrappy "1" inherits@2: version "2.0.3" resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.3.tgz#633c2c83e3da42a502f52466022480f4208261de" minimatch@^3.0.4: version "3.0.4" resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.0.4.tgz#5166e286457f03306064be5497e8dbb0c3d32083" dependencies: brace-expansion "^1.1.7" minimist@0.0.8: version "0.0.8" resolved "https://registry.yarnpkg.com/minimist/-/minimist-0.0.8.tgz#857fcabfc3397d2625b8228262e86aa7a011b05d" minimist@^1.2.0: version "1.2.0" resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.0.tgz#a35008b20f41383eec1fb914f4cd5df79a264284" mkdirp@^0.5.1: version "0.5.1" resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-0.5.1.tgz#30057438eac6cf7f8c4767f38648d6697d75c903" dependencies: minimist "0.0.8" once@^1.3.0: version "1.4.0" resolved "https://registry.yarnpkg.com/once/-/once-1.4.0.tgz#583b1aa775961d4b113ac17d9c50baef9dd76bd1" dependencies: wrappy "1" path-is-absolute@^1.0.0: version "1.0.1" resolved "https://registry.yarnpkg.com/path-is-absolute/-/path-is-absolute-1.0.1.tgz#174b9268735534ffbc7ace6bf53a5a9e1b5c5f5f" wrappy@1: version "1.0.2" resolved "https://registry.yarnpkg.com/wrappy/-/wrappy-1.0.2.tgz#b5243d8f3ec1aa35f1364605bc0d1036e30ab69f"