Skip to content

Commit b914042

Browse files
committed
Merge branch 'main' into subscription
2 parents 1d0255a + 4e63a0c commit b914042

File tree

9 files changed

+108
-21
lines changed

9 files changed

+108
-21
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
- name: Use Node.js latest
1919
uses: actions/setup-node@v3
2020
with:
21-
node-version: 17
21+
node-version: 20
2222
- name: npm install, build, and test
2323
# Note, we run bench and cov just to make sure they run successfully, but we're not doing anything with the result yet.
2424
run: |

README.md

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,30 @@ compiler).
1313

1414
## Index
1515

16+
- [Demo](#demo)
1617
- [Install](#install)
1718
- [Usage](#usage)
1819
- [Documentation](#documentation)
1920
- [Benchmarks](#benchmarks)
2021
- [Tests](#tests)
2122

23+
## Demo
24+
25+
[Live demo on CodePen.](https://codepen.io/trusktr/pen/abMLVxa?editors=1010)
26+
2227
## Install
2328

29+
### Local Install
30+
2431
Install using NPM:
2532

2633
```sh
2734
npm install @lume/kiwi
2835
```
2936

30-
then import it into your project:
31-
32-
```js
33-
import * as kiwi from '@lume/kiwi'
34-
35-
console.log(kiwi)
36-
37-
// ...use kiwi...
38-
```
39-
4037
If you have a plain web app with no build, or a non-browser JS runtime that also
4138
supports import maps like Deno, you'll need to add `@lume/kiwi` to your
42-
`importmap` script so that the browser knows where to import kiwi from. F.e.
39+
`importmap` script so that the browser or JS runtime knows where to import kiwi from. F.e.
4340
something like this:
4441

4542
```html
@@ -52,8 +49,32 @@ something like this:
5249
</script>
5350
```
5451

52+
### CDN Install
53+
54+
Note, if using importmaps and native ES Modules in a browser, or in a JS runtime like Deno, you can get Kiwi directly from the UNPKG CDN without installing it locally (just as the [live CodePen demo](#demo) does):
55+
56+
```html
57+
<script type="importmap">
58+
{
59+
"imports": {
60+
"@lume/kiwi": "https://unpkg.com/@lume/[email protected]/dist/kiwi.js"
61+
}
62+
}
63+
</script>
64+
```
65+
5566
## Usage
5667

68+
After installing, import kiwi into your project:
69+
70+
```js
71+
import * as kiwi from '@lume/kiwi'
72+
73+
console.log(kiwi)
74+
75+
// ...use kiwi...
76+
```
77+
5778
The following example creates a solver which automatically calculates a width based on some constraints:
5879

5980
```js
@@ -76,6 +97,16 @@ solver.addConstraint(new kiwi.Constraint(new kiwi.Expression([-1, right], left,
7697
solver.updateVariables()
7798

7899
console.assert(right.value() === 500)
100+
101+
// later, update the constraints and re-calculate
102+
setTimeout(() => {
103+
solver.suggestValue(left, 200)
104+
solver.suggestValue(width, 600)
105+
106+
solver.updateVariables() // update
107+
108+
console.assert(right.value() === 800)
109+
}, 2000)
79110
```
80111

81112
## Documentation
@@ -84,7 +115,7 @@ console.assert(right.value() === 500)
84115

85116
## Benchmarks
86117

87-
To run the benchmark in the browser, [just visit this page](https://rawgit.com/IjzerenHein/kiwi/master/bench/index.html).
118+
To run the benchmark in the browser, [just visit this page](https://raw.githack.com/lume/kiwi/main/bench/index.html).
88119

89120
To run the benchmark locally using nodejs, _clone or download this repository_ and execute the following steps:
90121

@@ -111,7 +142,7 @@ Fastest is kiwi (± 2.29x faster)
111142

112143
## Tests
113144

114-
To run the tests in the browser, [just visit this page](https://rawgit.com/IjzerenHein/kiwi/master/test/index.html).
145+
To run the tests in the browser, [just visit this page](https://raw.githack.com/lume/kiwi/main/test/index.html).
115146

116147
To run the tests locally using nodejs, _clone or download this repository_ and execute the following steps:
117148

@@ -135,12 +166,11 @@ open a pull request!
135166

136167
[![License](https://img.shields.io/badge/license-BDS%203--clause-brightgreen)](<https://tldrlegal.com/license/bsd-3-clause-license-(revised)>)
137168

138-
<!--
139-
TODO
140-
141169
## Status
142170

143-
[![Build Status](https://travis-ci.org/IjzerenHein/kiwi.js.svg?branch=master)](https://travis-ci.org/IjzerenHein/kiwi.js)
144-
[![codecov](https://codecov.io/gh/IjzerenHein/kiwi.js/branch/master/graph/badge.svg)](https://codecov.io/gh/IjzerenHein/kiwi.js)
171+
[![Build Status](https://github.com/lume/kiwi/actions/workflows/tests.yml/badge.svg)](https://github.com/lume/kiwi/actions/workflows/tests.yml)
145172

173+
<!--
174+
TODO coverage status
175+
[![codecov](https://codecov.io/gh/IjzerenHein/kiwi.js/branch/master/graph/badge.svg)](https://codecov.io/gh/IjzerenHein/kiwi.js)
146176
-->

dist/solver.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ export declare class Solver {
4444
* @return {Bool} true or false
4545
*/
4646
hasConstraint(constraint: Constraint): boolean;
47+
/**
48+
* Get an array of the current constraints.
49+
*
50+
* @return {Constraint[]}
51+
*/
52+
getConstraints(): Constraint[];
4753
/**
4854
* Add an edit variable to the solver.
4955
*

dist/solver.d.ts.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/solver.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,14 @@ export class Solver {
124124
hasConstraint(constraint) {
125125
return this._cnMap.contains(constraint);
126126
}
127+
/**
128+
* Get an array of the current constraints.
129+
*
130+
* @return {Constraint[]}
131+
*/
132+
getConstraints() {
133+
return this._cnMap.array.map(({ first }) => first);
134+
}
127135
/**
128136
* Add an edit variable to the solver.
129137
*

docs/Kiwi.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ console.assert(centerX.value() === 250)
8080
- [.addConstraint(constraint)](#module_@lume/kiwi..Solver+addConstraint)
8181
- [.removeConstraint(constraint)](#module_@lume/kiwi..Solver+removeConstraint)
8282
- [.hasConstraint(constraint)](#module_@lume/kiwi..Solver+hasConstraint) ⇒ <code>Bool</code>
83+
- [.getConstraints()](#module_@lume/kiwi..Solver+getConstraints) ⇒ <code>[ &#x27;Array&#x27; ].&lt;Constraint&gt;</code>
8384
- [.addEditVariable(variable, strength)](#module_@lume/kiwi..Solver+addEditVariable)
8485
- [.removeEditVariable(variable)](#module_@lume/kiwi..Solver+removeEditVariable)
8586
- [.hasEditVariable(variable)](#module_@lume/kiwi..Solver+hasEditVariable) ⇒ <code>Bool</code>
@@ -430,6 +431,7 @@ The constraint solver class.
430431
- [.addConstraint(constraint)](#module_@lume/kiwi..Solver+addConstraint)
431432
- [.removeConstraint(constraint)](#module_@lume/kiwi..Solver+removeConstraint)
432433
- [.hasConstraint(constraint)](#module_@lume/kiwi..Solver+hasConstraint) ⇒ <code>Bool</code>
434+
- [.getConstraints()](#module_@lume/kiwi..Solver+getConstraints) ⇒ <code>[ &#x27;Array&#x27; ].&lt;Constraint&gt;</code>
433435
- [.addEditVariable(variable, strength)](#module_@lume/kiwi..Solver+addEditVariable)
434436
- [.removeEditVariable(variable)](#module_@lume/kiwi..Solver+removeEditVariable)
435437
- [.hasEditVariable(variable)](#module_@lume/kiwi..Solver+hasEditVariable) ⇒ <code>Bool</code>
@@ -501,6 +503,13 @@ Test whether the solver contains the constraint.
501503
| ---------- | ----------------------- | ---------------------- |
502504
| constraint | <code>Constraint</code> | Constraint to test for |
503505

506+
<a name="module_@lume/kiwi..Solver+getConstraints"></a>
507+
508+
### solver.getConstraints() ⇒ <code>[ &#x27;Array&#x27; ].&lt;Constraint&gt;</code>
509+
510+
Get an array of the current constraints.
511+
512+
**Kind**: instance method of [<code>Solver</code>](#module_@lume/kiwi..Solver)
504513
<a name="module_@lume/kiwi..Solver+addEditVariable"></a>
505514

506515
### solver.addEditVariable(variable, strength)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@lume/kiwi",
33
"description": "High speed Cassowary constraint solver in JavaScript.",
4-
"version": "0.4.1",
4+
"version": "0.4.3",
55
"homepage": "https://github.com/lume/kiwi",
66
"repository": "https://github.com/lume/kiwi",
77
"type": "module",

src/solver.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,15 @@ export class Solver {
143143
return this._cnMap.contains(constraint)
144144
}
145145

146+
/**
147+
* Get an array of the current constraints.
148+
*
149+
* @return {Constraint[]}
150+
*/
151+
public getConstraints(): Constraint[] {
152+
return this._cnMap.array.map(({first}) => first)
153+
}
154+
146155
/**
147156
* Add an edit variable to the solver.
148157
*

test/main.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,31 @@ describe('import kiwi', function () {
205205
assert.equal(err.message, 'unsatisfiable constraint')
206206
}
207207
})
208+
it('solver.addConstraint() => solver.getConstraints()', function () {
209+
solver = new kiwi.Solver()
210+
var width = new kiwi.Variable()
211+
var width2 = new kiwi.Variable()
212+
var cn_1 = new kiwi.Constraint(new kiwi.Expression(width, 100), kiwi.Operator.Eq)
213+
solver.addConstraint(cn_1)
214+
var cn_2 = new kiwi.Constraint(new kiwi.Expression(width2, 100), kiwi.Operator.Eq)
215+
solver.addConstraint(cn_2)
216+
var cns = solver.getConstraints()
217+
assert(cns.indexOf(cn_1) > -1)
218+
assert(cns.indexOf(cn_2) > -1)
219+
})
220+
it('solver.removeConstraint() => solver.getConstraints()', function () {
221+
solver = new kiwi.Solver()
222+
var width = new kiwi.Variable()
223+
var width2 = new kiwi.Variable()
224+
var cn_1 = new kiwi.Constraint(new kiwi.Expression(width, 100), kiwi.Operator.Eq)
225+
solver.addConstraint(cn_1)
226+
var cn_2 = new kiwi.Constraint(new kiwi.Expression(width2, 100), kiwi.Operator.Eq)
227+
solver.addConstraint(cn_2)
228+
solver.removeConstraint(cn_1)
229+
var cns = solver.getConstraints()
230+
assert(cns.indexOf(cn_1) === -1)
231+
assert(cns.indexOf(cn_2) > -1)
232+
})
208233
})
209234

210235
describe('Constraint raw syntax: (expr, operator, undefined, strength)', function () {

0 commit comments

Comments
 (0)