diff --git a/Gruntfile.js b/Gruntfile.js deleted file mode 100644 index 08532ef..0000000 --- a/Gruntfile.js +++ /dev/null @@ -1,118 +0,0 @@ -/*global module:false*/ -/*eslint strict:false, quotes: [2, "single"] */ -module.exports = function(grunt) { - - // Project configuration. - grunt.initConfig({ - tslint: { - files: { - src: ['src/*.ts'] - } - }, - 'string-replace': { - dist: { - files: { - 'tmp/kiwi-no-internal-modules.js': 'lib/kiwi.js' - }, - options: { - // Generating docs for compiled Typescript code isn't easy... - // The following replacements were needed for jsdoc2md to do - // its processing correctly. - replacements: [{ - pattern: /\(function\s\(kiwi\)\s{/ig, - replacement: '' - }, { - pattern: /}\)\(Operator\s=\skiwi\.Operator\s\|\|\s\(kiwi\.Operator\s=\s{}\)\);/ig, - replacement: '' - }, { - }, { - pattern: /}\)\(kiwi\s\|\|\s\(kiwi\s=\s{}\)\);/ig, - replacement: '' - }, { - pattern: /\(function\s\(Operator\)\s{/ig, - replacement: 'var Operator;' - }, { - pattern: /}\)\(kiwi.Operator\s\|\|\s\(kiwi.Operator\s\=\s{}\)\);/ig, - replacement: '' - }, { - pattern: /\(function\s\(Strength\)\s{/ig, - replacement: '' - }, { - pattern: /}\)\(Strength\s\=\skiwi.Strength\s\|\|\s\(kiwi.Strength\s\=\s{}\)\);/ig, - replacement: '' - }, { - pattern: /function\screate\(a,\sb,\sc,\sw\)\s{/ig, - replacement: 'Strength.create = function(a, b, c, w) {' - }] - } - } - }, - jsdoc2md: { - output: { - options: { - 'global-index-format': 'none', - 'module-index-format': 'none' - }, - src: 'tmp/kiwi-no-internal-modules.js', - dest: 'docs/Kiwi.md' - } - }, - exec: { - build: 'node_modules/.bin/tsc --noImplicitAny -m amd -d -out lib/kiwi.js src/kiwi.ts' - }, - concat: { - extras: { - src: ['thirdparty/tsu.js', 'lib/kiwi.js'], - dest: 'lib/kiwi.js', - }, - }, - umd: { - all: { - options: { - src: 'lib/kiwi.js', - objectToExport: 'kiwi' - } - } - }, - uglify: { - dist: { - src: './lib/kiwi.js', - dest: './lib/kiwi.min.js' - } - }, - usebanner: { - minified: { - options: { - position: 'top', - banner: - '/*-----------------------------------------------------------------------------\n' + - '| Copyright (c) 2014-2018, Nucleic Development Team & H. Rutjes.\n' + - '|\n' + - '| Distributed under the terms of the Modified BSD License.\n' + - '|\n' + - '| The full license is in the file COPYING.txt, distributed with this software.\n' + - '-----------------------------------------------------------------------------*/' - }, - files: { - src: ['lib/kiwi.min.js'] - } - } - } - }); - - // These plugins provide necessary tasks. - grunt.loadNpmTasks('grunt-jsdoc-to-markdown'); - grunt.loadNpmTasks('grunt-tslint'); - grunt.loadNpmTasks('grunt-banner'); - grunt.loadNpmTasks('grunt-contrib-uglify'); - grunt.loadNpmTasks('grunt-exec'); - grunt.loadNpmTasks('grunt-umd'); - grunt.loadNpmTasks('grunt-contrib-concat'); - grunt.loadNpmTasks('grunt-string-replace'); - - // Tasks - grunt.registerTask('lint', ['tslint']); - grunt.registerTask('doc', ['string-replace', 'jsdoc2md']); - grunt.registerTask('build', ['exec:build', 'doc', 'concat', 'umd', 'minify']); - grunt.registerTask('minify', ['uglify', 'usebanner']); -}; diff --git a/docs/Kiwi.md b/docs/Kiwi.md index 83616b0..081c013 100644 --- a/docs/Kiwi.md +++ b/docs/Kiwi.md @@ -34,15 +34,10 @@ solver.updateVariables(); assert(centerX.value(), 250); ``` -##API Documentation +## API Documentation * [kiwi](#module_kiwi) - * [~Constraint](#module_kiwi..Constraint) - * [new Constraint(expression, operator, [rhs], [strength])](#new_module_kiwi..Constraint_new) - * [.expression()](#module_kiwi..Constraint+expression) ⇒ Expression - * [.op()](#module_kiwi..Constraint+op) ⇒ Operator - * [.strength()](#module_kiwi..Constraint+strength) ⇒ Number * [~Variable](#module_kiwi..Variable) * [new Variable([name])](#new_module_kiwi..Variable_new) * [.name()](#module_kiwi..Variable+name) ⇒ String @@ -64,6 +59,11 @@ assert(centerX.value(), 250); * [.medium](#module_kiwi..Strength.medium) * [.weak](#module_kiwi..Strength.weak) * [.create(a, b, c, [w])](#module_kiwi..Strength.create) ⇒ Number + * [~Constraint](#module_kiwi..Constraint) + * [new Constraint(expression, operator, [rhs], [strength])](#new_module_kiwi..Constraint_new) + * [.expression()](#module_kiwi..Constraint+expression) ⇒ Expression + * [.op()](#module_kiwi..Constraint+op) ⇒ Operator + * [.strength()](#module_kiwi..Constraint+strength) ⇒ Number * [~Solver](#module_kiwi..Solver) * [new Solver()](#new_module_kiwi..Solver_new) * [.createConstraint(lhs, operator, rhs, [strength])](#module_kiwi..Solver+createConstraint) @@ -77,57 +77,11 @@ assert(centerX.value(), 250); * [.updateVariables()](#module_kiwi..Solver+updateVariables) * [~Operator](#module_kiwi..Operator) : enum - - -### kiwi~Constraint -**Kind**: inner class of [kiwi](#module_kiwi) - -* [~Constraint](#module_kiwi..Constraint) - * [new Constraint(expression, operator, [rhs], [strength])](#new_module_kiwi..Constraint_new) - * [.expression()](#module_kiwi..Constraint+expression) ⇒ Expression - * [.op()](#module_kiwi..Constraint+op) ⇒ Operator - * [.strength()](#module_kiwi..Constraint+strength) ⇒ Number - - - -#### new Constraint(expression, operator, [rhs], [strength]) -A linear constraint equation. - -A constraint equation is composed of an expression, an operator, -and a strength. The RHS of the equation is implicitly zero. - - -| Param | Type | Default | Description | -| --- | --- | --- | --- | -| expression | Expression | | The constraint expression (LHS). | -| operator | Operator | | The equation operator. | -| [rhs] | Expression | | Right hand side of the expression. | -| [strength] | Number | Strength.required | The strength of the constraint. | - - - -#### constraint.expression() ⇒ Expression -Returns the expression of the constraint. - -**Kind**: instance method of [Constraint](#module_kiwi..Constraint) -**Returns**: Expression - expression - - -#### constraint.op() ⇒ Operator -Returns the relational operator of the constraint. - -**Kind**: instance method of [Constraint](#module_kiwi..Constraint) -**Returns**: Operator - linear constraint operator - - -#### constraint.strength() ⇒ Number -Returns the strength of the constraint. - -**Kind**: instance method of [Constraint](#module_kiwi..Constraint) -**Returns**: Number - strength ### kiwi~Variable +The primary user constraint variable. + **Kind**: inner class of [kiwi](#module_kiwi) * [~Variable](#module_kiwi..Variable) @@ -143,8 +97,6 @@ Returns the strength of the constraint. #### new Variable([name]) -The primary user constraint variable. - | Param | Type | Default | Description | | --- | --- | --- | --- | @@ -228,6 +180,17 @@ Creates a new Expression by dividing with a fixed number. ### kiwi~Expression +An expression of variable terms and a constant. + +The constructor accepts an arbitrary number of parameters, +each of which must be one of the following types: + - number + - Variable + - Expression + - 2-tuple of [number, Variable|Expression] + +The parameters are summed. The tuples are multiplied. + **Kind**: inner class of [kiwi](#module_kiwi) * [~Expression](#module_kiwi..Expression) @@ -240,17 +203,6 @@ Creates a new Expression by dividing with a fixed number. #### new Expression(...args) -An expression of variable terms and a constant. - -The constructor accepts an arbitrary number of parameters, -each of which must be one of the following types: - - number - - Variable - - Expression - - 2-tuple of [number, Variable|Expression] - -The parameters are summed. The tuples are multiplied. - | Param | Type | | --- | --- | @@ -350,16 +302,66 @@ Create a new symbolic strength. **Kind**: static method of [Strength](#module_kiwi..Strength) **Returns**: Number - strength -| Param | Type | Description | -| --- | --- | --- | -| a | Number | strong | -| b | Number | medium | -| c | Number | weak | -| [w] | Number | weight | +| Param | Type | Default | Description | +| --- | --- | --- | --- | +| a | Number | | strong | +| b | Number | | medium | +| c | Number | | weak | +| [w] | Number | 1 | weight | + + +### kiwi~Constraint +A linear constraint equation. + +A constraint equation is composed of an expression, an operator, +and a strength. The RHS of the equation is implicitly zero. + +**Kind**: inner class of [kiwi](#module_kiwi) + +* [~Constraint](#module_kiwi..Constraint) + * [new Constraint(expression, operator, [rhs], [strength])](#new_module_kiwi..Constraint_new) + * [.expression()](#module_kiwi..Constraint+expression) ⇒ Expression + * [.op()](#module_kiwi..Constraint+op) ⇒ Operator + * [.strength()](#module_kiwi..Constraint+strength) ⇒ Number + + + +#### new Constraint(expression, operator, [rhs], [strength]) + +| Param | Type | Default | Description | +| --- | --- | --- | --- | +| expression | Expression | | The constraint expression (LHS). | +| operator | Operator | | The equation operator. | +| [rhs] | Expression | | Right hand side of the expression. | +| [strength] | Number | Strength.required | The strength of the constraint. | + + + +#### constraint.expression() ⇒ Expression +Returns the expression of the constraint. + +**Kind**: instance method of [Constraint](#module_kiwi..Constraint) +**Returns**: Expression - expression + + +#### constraint.op() ⇒ Operator +Returns the relational operator of the constraint. + +**Kind**: instance method of [Constraint](#module_kiwi..Constraint) +**Returns**: Operator - linear constraint operator + + +#### constraint.strength() ⇒ Number +Returns the strength of the constraint. + +**Kind**: instance method of [Constraint](#module_kiwi..Constraint) +**Returns**: Number - strength ### kiwi~Solver +The constraint solver class. + **Kind**: inner class of [kiwi](#module_kiwi) * [~Solver](#module_kiwi..Solver) @@ -377,7 +379,7 @@ Create a new symbolic strength. #### new Solver() -The constraint solver class. +Construct a new Solver. diff --git a/es/constraint.d.ts b/es/constraint.d.ts index 57ff90c..81f6c4c 100644 --- a/es/constraint.d.ts +++ b/es/constraint.d.ts @@ -1,42 +1,5 @@ import { Expression } from "./expression"; import { Variable } from "./variable"; -/** - * Kiwi is an efficient implementation of the Cassowary constraint solving - * algorithm, based on the seminal Cassowary paper. - * It is *not* a refactoring or port of the original C++ solver, but - * has been designed from the ground up to be lightweight and fast. - * - * **Example** - * - * ```javascript - * var kiwi = require('kiwi'); - * - * // Create a solver - * var solver = new kiwi.Solver(); - * - * // Create and add some editable variables - * var left = new kiwi.Variable(); - * var width = new kiwi.Variable(); - * solver.addEditVariable(left, kiwi.Strength.strong); - * solver.addEditVariable(width, kiwi.Strength.strong); - * - * // Create a variable calculated through a constraint - * var centerX = new kiwi.Variable(); - * var expr = new kiwi.Expression([-1, centerX], left, [0.5, width]); - * solver.addConstraint(new kiwi.Constraint(expr, kiwi.Operator.Eq, kiwi.Strength.required)); - * - * // Suggest some values to the solver - * solver.suggestValue(left, 0); - * solver.suggestValue(width, 500); - * - * // Lets solve the problem! - * solver.updateVariables(); - * assert(centerX.value(), 250); - * ``` - * - * ##API Documentation - * @module kiwi - */ /** * An enum defining the linear constraint operators. * diff --git a/es/constraint.js b/es/constraint.js index 080b7f9..fb06e69 100644 --- a/es/constraint.js +++ b/es/constraint.js @@ -7,43 +7,6 @@ |----------------------------------------------------------------------------*/ import { Expression } from "./expression"; import { Strength } from "./strength"; -/** - * Kiwi is an efficient implementation of the Cassowary constraint solving - * algorithm, based on the seminal Cassowary paper. - * It is *not* a refactoring or port of the original C++ solver, but - * has been designed from the ground up to be lightweight and fast. - * - * **Example** - * - * ```javascript - * var kiwi = require('kiwi'); - * - * // Create a solver - * var solver = new kiwi.Solver(); - * - * // Create and add some editable variables - * var left = new kiwi.Variable(); - * var width = new kiwi.Variable(); - * solver.addEditVariable(left, kiwi.Strength.strong); - * solver.addEditVariable(width, kiwi.Strength.strong); - * - * // Create a variable calculated through a constraint - * var centerX = new kiwi.Variable(); - * var expr = new kiwi.Expression([-1, centerX], left, [0.5, width]); - * solver.addConstraint(new kiwi.Constraint(expr, kiwi.Operator.Eq, kiwi.Strength.required)); - * - * // Suggest some values to the solver - * solver.suggestValue(left, 0); - * solver.suggestValue(width, 500); - * - * // Lets solve the problem! - * solver.updateVariables(); - * assert(centerX.value(), 250); - * ``` - * - * ##API Documentation - * @module kiwi - */ /** * An enum defining the linear constraint operators. * diff --git a/es/expression.js b/es/expression.js index ea03c82..f553832 100644 --- a/es/expression.js +++ b/es/expression.js @@ -100,7 +100,7 @@ var Expression = /** @class */ (function () { return this._terms.size() == 0; }; Expression.prototype.toString = function () { - var result = this._terms._array.map(function (pair, idx) { + var result = this._terms.array.map(function (pair, idx) { return (pair.second + "*" + pair.first.toString()); }).join(" + "); if (!this.isConstant() && this._constant !== 0) { diff --git a/es/kiwi.d.ts b/es/kiwi.d.ts index 14efed0..6abec62 100644 --- a/es/kiwi.d.ts +++ b/es/kiwi.d.ts @@ -1,6 +1,5 @@ export * from "./constraint"; export * from "./expression"; -export * from "./maptype"; export * from "./solver"; export * from "./strength"; export * from "./variable"; diff --git a/es/kiwi.js b/es/kiwi.js index 17bb341..a13e872 100644 --- a/es/kiwi.js +++ b/es/kiwi.js @@ -7,7 +7,6 @@ |----------------------------------------------------------------------------*/ export * from "./constraint"; export * from "./expression"; -export * from "./maptype"; export * from "./solver"; export * from "./strength"; export * from "./variable"; diff --git a/es/maptype.d.ts b/es/maptype.d.ts index 96fa5a2..79b19ad 100644 --- a/es/maptype.d.ts +++ b/es/maptype.d.ts @@ -8,17 +8,17 @@ export declare function createMap { - _index: { + index: { [id: number]: number; }; - _array: Pair[]; + array: Pair[]; /** * Returns the number of items in the array. */ size(): number; /** - * Returns true if the array is empty. - */ + * Returns true if the array is empty. + */ empty(): boolean; /** * Returns the item at the given array index. @@ -27,10 +27,10 @@ declare class IndexedMap; /** - * Returns true if the key is in the array, false otherwise. - * - * @param key The key to locate in the array. - */ + * Returns true if the key is in the array, false otherwise. + * + * @param key The key to locate in the array. + */ contains(key: T): boolean; /** * Returns the pair associated with the given key, or undefined. @@ -69,21 +69,22 @@ declare class IndexedMap; } /** -* A class which defines a generic pair object. -*/ + * A class which defines a generic pair object. + * @private + */ declare class Pair { first: T; second: U; /** - * Construct a new Pair object. - * - * @param first The first item of the pair. - * @param second The second item of the pair. - */ + * Construct a new Pair object. + * + * @param first The first item of the pair. + * @param second The second item of the pair. + */ constructor(first: T, second: U); /** - * Create a copy of the pair. - */ + * Create a copy of the pair. + */ copy(): Pair; } export {}; diff --git a/es/maptype.js b/es/maptype.js index e29bb68..13d0e78 100644 --- a/es/maptype.js +++ b/es/maptype.js @@ -5,36 +5,25 @@ | | The full license is in the file COPYING.txt, distributed with this software. |----------------------------------------------------------------------------*/ -var __assign = (this && this.__assign) || function () { - __assign = Object.assign || function(t) { - for (var s, i = 1, n = arguments.length; i < n; i++) { - s = arguments[i]; - for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) - t[p] = s[p]; - } - return t; - }; - return __assign.apply(this, arguments); -}; export function createMap(compare) { return new IndexedMap(); } var IndexedMap = /** @class */ (function () { function IndexedMap() { - this._index = {}; - this._array = []; + this.index = {}; + this.array = []; } /** * Returns the number of items in the array. */ IndexedMap.prototype.size = function () { - return this._array.length; + return this.array.length; }; /** - * Returns true if the array is empty. - */ + * Returns true if the array is empty. + */ IndexedMap.prototype.empty = function () { - return this._array.length === 0; + return this.array.length === 0; }; /** * Returns the item at the given array index. @@ -42,15 +31,15 @@ var IndexedMap = /** @class */ (function () { * @param index The integer index of the desired item. */ IndexedMap.prototype.itemAt = function (index) { - return this._array[index]; + return this.array[index]; }; /** - * Returns true if the key is in the array, false otherwise. - * - * @param key The key to locate in the array. - */ + * Returns true if the key is in the array, false otherwise. + * + * @param key The key to locate in the array. + */ IndexedMap.prototype.contains = function (key) { - return this._index[key.id()] !== undefined; + return this.index[key.id()] !== undefined; }; /** * Returns the pair associated with the given key, or undefined. @@ -58,8 +47,8 @@ var IndexedMap = /** @class */ (function () { * @param key The key to locate in the array. */ IndexedMap.prototype.find = function (key) { - var i = this._index[key.id()]; - return i === undefined ? undefined : this._array[i]; + var i = this.index[key.id()]; + return i === undefined ? undefined : this.array[i]; }; /** * Returns the pair associated with the key if it exists. @@ -71,15 +60,15 @@ var IndexedMap = /** @class */ (function () { * @param factory The function which creates the default value. */ IndexedMap.prototype.setDefault = function (key, factory) { - var i = this._index[key.id()]; + var i = this.index[key.id()]; if (i === undefined) { var pair = new Pair(key, factory()); - this._index[key.id()] = this._array.length; - this._array.push(pair); + this.index[key.id()] = this.array.length; + this.array.push(pair); return pair; } else { - return this._array[i]; + return this.array[i]; } }; /** @@ -91,13 +80,14 @@ var IndexedMap = /** @class */ (function () { * @param value The value portion of the pair. */ IndexedMap.prototype.insert = function (key, value) { - var pair = new Pair(key, value), i = this._index[key.id()]; + var pair = new Pair(key, value); + var i = this.index[key.id()]; if (i === undefined) { - this._index[key.id()] = this._array.length; - this._array.push(pair); + this.index[key.id()] = this.array.length; + this.array.push(pair); } else { - this._array[i] = pair; + this.array[i] = pair; } return pair; }; @@ -107,14 +97,16 @@ var IndexedMap = /** @class */ (function () { * @param key The key to remove from the map. */ IndexedMap.prototype.erase = function (key) { - var i = this._index[key.id()]; - if (i === undefined) + var i = this.index[key.id()]; + if (i === undefined) { return undefined; - this._index[key.id()] = undefined; - var pair = this._array[i], last = this._array.pop(); + } + this.index[key.id()] = undefined; + var pair = this.array[i]; + var last = this.array.pop(); if (pair !== last) { - this._array[i] = last; - this._index[last.first.id()] = i; + this.array[i] = last; + this.index[last.first.id()] = i; } return pair; }; @@ -123,29 +115,34 @@ var IndexedMap = /** @class */ (function () { */ IndexedMap.prototype.copy = function () { var copy = new IndexedMap(); - copy._index = __assign({}, this._index); - copy._array = this._array.map(function (p) { return p.copy(); }); + for (var i = 0; i < this.array.length; i++) { + var pair = this.array[i].copy(); + copy.array[i] = pair; + copy.index[pair.first.id()] = i; + } return copy; }; return IndexedMap; }()); /** -* A class which defines a generic pair object. -*/ + * A class which defines a generic pair object. + * @private + */ +// tslint:disable: max-classes-per-file var Pair = /** @class */ (function () { /** - * Construct a new Pair object. - * - * @param first The first item of the pair. - * @param second The second item of the pair. - */ + * Construct a new Pair object. + * + * @param first The first item of the pair. + * @param second The second item of the pair. + */ function Pair(first, second) { this.first = first; this.second = second; } /** - * Create a copy of the pair. - */ + * Create a copy of the pair. + */ Pair.prototype.copy = function () { return new Pair(this.first, this.second); }; return Pair; }()); diff --git a/lib/kiwi.js b/lib/kiwi.js index 41bd979..3a89501 100644 --- a/lib/kiwi.js +++ b/lib/kiwi.js @@ -1,3 +1,50 @@ + +/*----------------------------------------------------------------------------- +| Copyright (c) 2014-2018, Nucleic Development Team & H. Rutjes. +| +| Distributed under the terms of the Modified BSD License. +| +| The full license is in the file COPYING.txt, distributed with this software. +-----------------------------------------------------------------------------*/ + +/** + * Kiwi is an efficient implementation of the Cassowary constraint solving + * algorithm, based on the seminal Cassowary paper. + * It is *not* a refactoring or port of the original C++ solver, but + * has been designed from the ground up to be lightweight and fast. + * + * **Example** + * + * ```javascript + * var kiwi = require('kiwi'); + * + * // Create a solver + * var solver = new kiwi.Solver(); + * + * // Create and add some editable variables + * var left = new kiwi.Variable(); + * var width = new kiwi.Variable(); + * solver.addEditVariable(left, kiwi.Strength.strong); + * solver.addEditVariable(width, kiwi.Strength.strong); + * + * // Create a variable calculated through a constraint + * var centerX = new kiwi.Variable(); + * var expr = new kiwi.Expression([-1, centerX], left, [0.5, width]); + * solver.addConstraint(new kiwi.Constraint(expr, kiwi.Operator.Eq, kiwi.Strength.required)); + * + * // Suggest some values to the solver + * solver.suggestValue(left, 0); + * solver.suggestValue(width, 500); + * + * // Lets solve the problem! + * solver.updateVariables(); + * assert(centerX.value(), 250); + * ``` + * + * ## API Documentation + * @module kiwi + */ + (function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : typeof define === 'function' && define.amd ? define(['exports'], factory) : @@ -11,36 +58,25 @@ | | The full license is in the file COPYING.txt, distributed with this software. |----------------------------------------------------------------------------*/ - var __assign = (undefined && undefined.__assign) || function () { - __assign = Object.assign || function(t) { - for (var s, i = 1, n = arguments.length; i < n; i++) { - s = arguments[i]; - for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) - t[p] = s[p]; - } - return t; - }; - return __assign.apply(this, arguments); - }; function createMap(compare) { return new IndexedMap(); } var IndexedMap = /** @class */ (function () { function IndexedMap() { - this._index = {}; - this._array = []; + this.index = {}; + this.array = []; } /** * Returns the number of items in the array. */ IndexedMap.prototype.size = function () { - return this._array.length; + return this.array.length; }; /** - * Returns true if the array is empty. - */ + * Returns true if the array is empty. + */ IndexedMap.prototype.empty = function () { - return this._array.length === 0; + return this.array.length === 0; }; /** * Returns the item at the given array index. @@ -48,15 +84,15 @@ * @param index The integer index of the desired item. */ IndexedMap.prototype.itemAt = function (index) { - return this._array[index]; + return this.array[index]; }; /** - * Returns true if the key is in the array, false otherwise. - * - * @param key The key to locate in the array. - */ + * Returns true if the key is in the array, false otherwise. + * + * @param key The key to locate in the array. + */ IndexedMap.prototype.contains = function (key) { - return this._index[key.id()] !== undefined; + return this.index[key.id()] !== undefined; }; /** * Returns the pair associated with the given key, or undefined. @@ -64,8 +100,8 @@ * @param key The key to locate in the array. */ IndexedMap.prototype.find = function (key) { - var i = this._index[key.id()]; - return i === undefined ? undefined : this._array[i]; + var i = this.index[key.id()]; + return i === undefined ? undefined : this.array[i]; }; /** * Returns the pair associated with the key if it exists. @@ -77,15 +113,15 @@ * @param factory The function which creates the default value. */ IndexedMap.prototype.setDefault = function (key, factory) { - var i = this._index[key.id()]; + var i = this.index[key.id()]; if (i === undefined) { var pair = new Pair(key, factory()); - this._index[key.id()] = this._array.length; - this._array.push(pair); + this.index[key.id()] = this.array.length; + this.array.push(pair); return pair; } else { - return this._array[i]; + return this.array[i]; } }; /** @@ -97,13 +133,14 @@ * @param value The value portion of the pair. */ IndexedMap.prototype.insert = function (key, value) { - var pair = new Pair(key, value), i = this._index[key.id()]; + var pair = new Pair(key, value); + var i = this.index[key.id()]; if (i === undefined) { - this._index[key.id()] = this._array.length; - this._array.push(pair); + this.index[key.id()] = this.array.length; + this.array.push(pair); } else { - this._array[i] = pair; + this.array[i] = pair; } return pair; }; @@ -113,14 +150,16 @@ * @param key The key to remove from the map. */ IndexedMap.prototype.erase = function (key) { - var i = this._index[key.id()]; - if (i === undefined) + var i = this.index[key.id()]; + if (i === undefined) { return undefined; - this._index[key.id()] = undefined; - var pair = this._array[i], last = this._array.pop(); + } + this.index[key.id()] = undefined; + var pair = this.array[i]; + var last = this.array.pop(); if (pair !== last) { - this._array[i] = last; - this._index[last.first.id()] = i; + this.array[i] = last; + this.index[last.first.id()] = i; } return pair; }; @@ -129,29 +168,34 @@ */ IndexedMap.prototype.copy = function () { var copy = new IndexedMap(); - copy._index = __assign({}, this._index); - copy._array = this._array.map(function (p) { return p.copy(); }); + for (var i = 0; i < this.array.length; i++) { + var pair = this.array[i].copy(); + copy.array[i] = pair; + copy.index[pair.first.id()] = i; + } return copy; }; return IndexedMap; }()); /** - * A class which defines a generic pair object. - */ + * A class which defines a generic pair object. + * @private + */ + // tslint:disable: max-classes-per-file var Pair = /** @class */ (function () { /** - * Construct a new Pair object. - * - * @param first The first item of the pair. - * @param second The second item of the pair. - */ + * Construct a new Pair object. + * + * @param first The first item of the pair. + * @param second The second item of the pair. + */ function Pair(first, second) { this.first = first; this.second = second; } /** - * Create a copy of the pair. - */ + * Create a copy of the pair. + */ Pair.prototype.copy = function () { return new Pair(this.first, this.second); }; return Pair; }()); @@ -395,7 +439,7 @@ return this._terms.size() == 0; }; Expression.prototype.toString = function () { - var result = this._terms._array.map(function (pair, idx) { + var result = this._terms.array.map(function (pair, idx) { return (pair.second + "*" + pair.first.toString()); }).join(" + "); if (!this.isConstant() && this._constant !== 0) { @@ -1562,7 +1606,6 @@ exports.Constraint = Constraint; exports.Expression = Expression; - exports.createMap = createMap; exports.Solver = Solver; exports.Strength = Strength; exports.Variable = Variable; diff --git a/package.json b/package.json index 8ca2fca..75d3ad3 100644 --- a/package.json +++ b/package.json @@ -7,9 +7,11 @@ "module": "es/kiwi.js", "types": "es/kiwi.d.ts", "author": "Chris Colbert ", - "contributors": { - "name": "Hein Rutjes " - }, + "contributors": [ + { + "name": "Hein Rutjes " + } + ], "description": "Kiwi is a high speed implementation of the Cassowary constraint solving algorithm", "keywords": [ "cassowary", @@ -25,6 +27,7 @@ "files": [ "src", "lib", + "es", "COPYING.txt" ], "devDependencies": { @@ -32,28 +35,20 @@ "cassowary": "0.0.2", "chai": "^4.1.2", "codecov": "^3.0.0", - "grunt": "^1.0.2", - "grunt-banner": "^0.6.0", - "grunt-contrib-concat": "^1.0.1", - "grunt-contrib-uglify": "^3.3.0", - "grunt-exec": "^3.0.0", - "grunt-jsdoc-to-markdown": "^4.0.0", - "grunt-string-replace": "^1.3.1", - "grunt-tslint": "^5.0.1", - "grunt-umd": "^3.0.0", "istanbul": "^0.4.5", + "jsdoc-to-markdown": "^4.0.1", "mocha": "^3.1.2", "rollup": "^0.65.0", + "rollup-plugin-uglify": "^5.0.2", "tslint": "^5.9.1", "typescript": "^3.0.3" }, "scripts": { - "build": "grunt build", - "rollup": "tsc && rollup -c", + "build": "tsc && tsc -t es6 --outDir tmp/es && rollup -c", "test": "mocha", "cov": "istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -R test/main", - "doc": "grunt doc", - "lint": "grunt lint", + "doc": "jsdoc2md -f tmp/kiwi.js -m none -g none > docs/Kiwi.md", + "lint": "tslint -c tslint.json 'src/**/*.ts'", "bench": "node bench/main.js" } } diff --git a/rollup.config.js b/rollup.config.js index c15adf4..1e950e7 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -1,14 +1,92 @@ -export default { - input: 'es/kiwi.js', - output: { - file: 'lib/kiwi.js', - format: 'umd', - name: 'kiwi', - exports: 'named', +import { uglify } from 'rollup-plugin-uglify'; + +const license = ` +/*----------------------------------------------------------------------------- +| Copyright (c) 2014-2018, Nucleic Development Team & H. Rutjes. +| +| Distributed under the terms of the Modified BSD License. +| +| The full license is in the file COPYING.txt, distributed with this software. +-----------------------------------------------------------------------------*/ +`, + banner = license + ` +/** + * Kiwi is an efficient implementation of the Cassowary constraint solving + * algorithm, based on the seminal Cassowary paper. + * It is *not* a refactoring or port of the original C++ solver, but + * has been designed from the ground up to be lightweight and fast. + * + * **Example** + * + * \`\`\`javascript + * var kiwi = require('kiwi'); + * + * // Create a solver + * var solver = new kiwi.Solver(); + * + * // Create and add some editable variables + * var left = new kiwi.Variable(); + * var width = new kiwi.Variable(); + * solver.addEditVariable(left, kiwi.Strength.strong); + * solver.addEditVariable(width, kiwi.Strength.strong); + * + * // Create a variable calculated through a constraint + * var centerX = new kiwi.Variable(); + * var expr = new kiwi.Expression([-1, centerX], left, [0.5, width]); + * solver.addConstraint(new kiwi.Constraint(expr, kiwi.Operator.Eq, kiwi.Strength.required)); + * + * // Suggest some values to the solver + * solver.suggestValue(left, 0); + * solver.suggestValue(width, 500); + * + * // Lets solve the problem! + * solver.updateVariables(); + * assert(centerX.value(), 250); + * \`\`\` + * + * ## API Documentation + * @module kiwi + */ +`; + +// we generate three output formats: +// - UMD in lib/kiwi.js +// - minified UMD in lib/kiwi.min.js +// - a fully ES6 version in tmp/kiwi.js, used just as the input to jsdoc2md, as we have +// Typescript set to down-compile the others to ES5 for max compatibility, but +// Typescript's ES5-polyfills confuse jsdoc2md. + +const umd = { + input: 'es/kiwi.js', + output: { + file: 'lib/kiwi.js', + format: 'umd', + name: 'kiwi', + exports: 'named', + banner + } }, - onwarn: function (warning) { - // TS emits some helpers with code that causes rollup to carp. This disables that error. - if (warning.code === 'THIS_IS_UNDEFINED') return; - console.error(warning.message); - } -} \ No newline at end of file + minified = { + ...umd, + output: { + ...umd.output, + file: 'lib/kiwi.min.js' + }, + plugins: [ + uglify({ + output: { + preamble: license + } + }) + ] + }, + doc = { + input: 'tmp/es/kiwi.js', + output: { + ...umd.output, + file: 'tmp/kiwi.js', + format: 'es' + } + }; + +export default [ umd, minified, doc ]; \ No newline at end of file diff --git a/src/constraint.ts b/src/constraint.ts index 0495312..006f05f 100644 --- a/src/constraint.ts +++ b/src/constraint.ts @@ -10,44 +10,6 @@ import { Expression } from "./expression"; import { Strength } from "./strength"; import { Variable } from "./variable"; -/** - * Kiwi is an efficient implementation of the Cassowary constraint solving - * algorithm, based on the seminal Cassowary paper. - * It is *not* a refactoring or port of the original C++ solver, but - * has been designed from the ground up to be lightweight and fast. - * - * **Example** - * - * ```javascript - * var kiwi = require('kiwi'); - * - * // Create a solver - * var solver = new kiwi.Solver(); - * - * // Create and add some editable variables - * var left = new kiwi.Variable(); - * var width = new kiwi.Variable(); - * solver.addEditVariable(left, kiwi.Strength.strong); - * solver.addEditVariable(width, kiwi.Strength.strong); - * - * // Create a variable calculated through a constraint - * var centerX = new kiwi.Variable(); - * var expr = new kiwi.Expression([-1, centerX], left, [0.5, width]); - * solver.addConstraint(new kiwi.Constraint(expr, kiwi.Operator.Eq, kiwi.Strength.required)); - * - * // Suggest some values to the solver - * solver.suggestValue(left, 0); - * solver.suggestValue(width, 500); - * - * // Lets solve the problem! - * solver.updateVariables(); - * assert(centerX.value(), 250); - * ``` - * - * ##API Documentation - * @module kiwi - */ - /** * An enum defining the linear constraint operators. * diff --git a/src/kiwi.ts b/src/kiwi.ts index 24ad75d..3f19a97 100644 --- a/src/kiwi.ts +++ b/src/kiwi.ts @@ -8,7 +8,6 @@ export * from "./constraint"; export * from "./expression"; -export * from "./maptype"; export * from "./solver"; export * from "./strength"; export * from "./variable";