Skip to content

Commit 919d003

Browse files
authored
fix scale.domain implementation (#381)
* fix scale.domain implementation * chore: make sure tests are run in CI * install @changeset/changeset * add changeset
1 parent 4e4df90 commit 919d003

File tree

8 files changed

+81
-15
lines changed

8 files changed

+81
-15
lines changed

.changeset/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Changesets
2+
3+
Hello and welcome! This folder has been automatically generated by `@changesets/cli`, a build tool that works
4+
with multi-package repos, or single-package repos to help you version and publish your code. You can
5+
find the full documentation for it [in our repository](https://github.com/changesets/changesets)
6+
7+
We have a quick list of common questions to get you started engaging with this project in
8+
[our documentation](https://github.com/changesets/changesets/blob/main/docs/common-questions.md)

.changeset/config.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"$schema": "https://unpkg.com/@changesets/config@3.1.2/schema.json",
3+
"changelog": "@changesets/cli/changelog",
4+
"commit": false,
5+
"fixed": [],
6+
"linked": [],
7+
"access": "restricted",
8+
"baseBranch": "main",
9+
"updateInternalDependencies": "patch",
10+
"ignore": []
11+
}

.changeset/scale-domain-no-args.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'chroma-js': minor
3+
---
4+
5+
scale.domain now returns the original domain array when called with no arguments

.github/workflows/ci.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
name: CI
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- main
7+
push:
8+
branches:
9+
- main
10+
11+
jobs:
12+
lint-and-test:
13+
runs-on: ubuntu-latest
14+
env:
15+
CI: true
16+
17+
steps:
18+
- name: Checkout repository
19+
uses: actions/checkout@v4
20+
21+
- name: Set up pnpm
22+
uses: pnpm/action-setup@v4
23+
with:
24+
version: 10.24.0
25+
26+
- name: Set up Node.js
27+
uses: actions/setup-node@v4
28+
with:
29+
node-version: 20
30+
cache: pnpm
31+
32+
- name: Install dependencies
33+
run: pnpm install --frozen-lockfile
34+
35+
- name: Lint
36+
run: pnpm lint
37+
38+
- name: Test
39+
run: pnpm test -- --run

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
chroma.js - JavaScript library for color conversions
22

3-
Copyright (c) 2011-2024, Gregor Aisch
3+
Copyright (c) 2011-2025, Gregor Aisch
44
All rights reserved.
55

66
Redistribution and use in source and binary forms, with or without

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
},
5252
"main": "./index.js",
5353
"scripts": {
54-
"prepublishOnly": "npm test -- --run && node .bin/update-version.cjs && npm run build",
54+
"prepublishOnly": "npm test -- --run && node .bin/update-version.cjs && npm run build && npm run docs",
5555
"build": "rollup -c && cross-env DEV=1 rollup -c ",
5656
"docs": "cd docs && make",
5757
"docs-preview": "cd docs && make && make preview",

src/generator/scale.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export default function (colors) {
1212
let _nacol = chroma('#ccc');
1313
let _spread = 0;
1414
// const _fixed = false;
15+
let _positions = [0, 1];
1516
let _domain = [0, 1];
1617
let _pos = [];
1718
let _padding = [0, 0];
@@ -180,9 +181,9 @@ export default function (colors) {
180181
if (classes != null) {
181182
if (type(classes) === 'array') {
182183
_classes = classes;
183-
_domain = [classes[0], classes[classes.length - 1]];
184+
_positions = [classes[0], classes[classes.length - 1]];
184185
} else {
185-
const d = chroma.analyze(_domain);
186+
const d = chroma.analyze(_positions);
186187
if (classes === 0) {
187188
_classes = [d.min, d.max];
188189
} else {
@@ -196,8 +197,11 @@ export default function (colors) {
196197

197198
f.domain = function (domain) {
198199
if (!arguments.length) {
199-
return _pos.map(p => _min + p * (_max - _min));;
200+
// return original domain
201+
return _domain;
200202
}
203+
// store original domain so we can return it later
204+
_domain = domain.slice(0);
201205
_min = domain[0];
202206
_max = domain[domain.length - 1];
203207
_pos = [];
@@ -228,7 +232,7 @@ export default function (colors) {
228232
}
229233
}
230234
}
231-
_domain = [_min, _max];
235+
_positions = [_min, _max];
232236
return f;
233237
};
234238

@@ -324,8 +328,8 @@ export default function (colors) {
324328
} else if (numColors === 1) {
325329
result = [f(0.5)];
326330
} else if (numColors > 1) {
327-
const dm = _domain[0];
328-
const dd = _domain[1] - dm;
331+
const dm = _positions[0];
332+
const dd = _positions[1] - dm;
329333
result = __range__(0, numColors, false).map((i) =>
330334
f(dm + (i / (numColors - 1)) * dd)
331335
);
@@ -342,7 +346,7 @@ export default function (colors) {
342346
samples.push((_classes[i - 1] + _classes[i]) * 0.5);
343347
}
344348
} else {
345-
samples = _domain;
349+
samples = _positions;
346350
}
347351
result = samples.map((v) => f(v));
348352
}

test/scales.test.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -413,22 +413,21 @@ describe('Some tests for scale()', () => {
413413

414414
describe('multi-stop domain with matching ramp stops', () => {
415415
const f = scale(['yellow', 'red', 'black']).domain([0, 25, 100]);
416-
416+
417417
it('returns the original domain positions', () => {
418418
expect(f.domain()).toEqual([0, 25, 100]);
419419
});
420-
420+
421421
it('maps first stop', () => {
422422
expect(f(0).hex()).toBe('#ffff00');
423423
});
424-
424+
425425
it('maps mid stop', () => {
426-
expect(f(25).hex()).toBe('#ff8080');
426+
expect(f(25).hex()).toBe('#ff0000');
427427
});
428-
428+
429429
it('maps last stop', () => {
430430
expect(f(100).hex()).toBe('#000000');
431431
});
432432
});
433-
434433
});

0 commit comments

Comments
 (0)