Skip to content

Commit f02a8e6

Browse files
dankogaiclaude
andcommitted
Build with rollup like js-base64
One TypeScript source now yields all three formats via rollup: combinatorics.js (ESM), commonjs/combinatorics.js and umd/combinatorics.js -- the UMD build now provides a proper window.Combinatorics browser global. The version string is injected from package.json at build time, and the npm version hook keeps README CDN URLs in sync. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent a35bfef commit f02a8e6

13 files changed

Lines changed: 153 additions & 121 deletions

Makefile

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,10 @@
1-
PJ=package.json
2-
TS=combinatorics.ts
3-
JS=combinatorics.js
4-
MJS=combinatorics.mjs
5-
DTS=combinatorics.d.ts
6-
COMMONJS_DIR=commonjs
7-
COMMONJS=$(COMMONJS_DIR)/$(JS)
8-
UMD_DIR=umd
9-
UMD=$(UMD_DIR)/$(JS)
1+
all:
2+
npm run build
103

11-
all: $(PJ) $(JS) $(COMMONJS) $(UMD)
12-
13-
$(JS): $(PJ) $(TS)
14-
tsc -d --module nodenext $(TS)
15-
16-
$(COMMONJS): $(PJ) $(TS)
17-
tsc --module commonjs --outDir $(COMMONJS_DIR) --target es2020 $(TS)
18-
19-
$(UMD): $(PJ) $(TS)
20-
-tsc --module umd --outDir $(UMD_DIR) --target es2020 $(TS) > /dev/null
21-
22-
test: all
23-
mocha
4+
test:
5+
npm test
246

257
clean:
26-
-rm $(DTS) $(MJS) $(JS) $(COMMONJS) $(UMD)
8+
npm run clean
279

10+
.PHONY: all test clean

README.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,10 @@ You don't even have to install if you `import` from CDNs.
5050
import * as $C from 'https://cdn.jsdelivr.net/npm/js-combinatorics@2.1.2/combinatorics.min.js';
5151
```
5252

53-
Since this is an ES6 module, `type="module"` is required the `<script>` tags. of your HTML files. But you can make it globally available as follows.
53+
Or plain old `<script>` tags — the UMD version `umd/combinatorics.js` makes `Combinatorics` globally available.
5454

5555
```html
56-
<script type="module">
57-
import * as $C from 'combinatorics.js';
58-
window.Combinatorics = $C;
59-
</script>
56+
<script src="https://cdn.jsdelivr.net/npm/js-combinatorics@2.1.2/umd/combinatorics.min.js"></script>
6057
<script>
6158
// now you can access Combinatorics
6259
let c = new Combinatorics.Combination('abcdefgh', 4);
@@ -581,7 +578,7 @@ ditto
581578
* module. `import` instead of `require`.
582579
* `BigInt` where possible
583580

584-
And from version 1.2 it is written in TypeScript. `combinatorics.js` and `combinatorics.d.ts` are compiled from `combinatorics.ts`.
581+
And from version 1.2 it is written in TypeScript. `combinatorics.js` and `combinatorics.d.ts` are compiled from `combinatorics.ts`.
585582

586583
APIs will change accordingly. Old versions are available in the `version0` branch.
587584

combinatorics.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* @link: http://en.wikipedia.org/wiki/Factorial_number_system
1313
* @link: https://en.wikipedia.org/wiki/Combinatorial_number_system
1414
*/
15-
export declare const version = "2.1.2";
15+
export declare const version: string;
1616
/**
1717
* BigInt Workaround
1818
*

combinatorics.js

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
//
2+
// THIS FILE IS AUTOMATICALLY GENERATED! DO NOT EDIT BY HAND!
3+
//
14
/**
25
* combinatorics.js
36
*
@@ -12,13 +15,13 @@
1215
* @link: http://en.wikipedia.org/wiki/Factorial_number_system
1316
* @link: https://en.wikipedia.org/wiki/Combinatorial_number_system
1417
*/
15-
export const version = '2.1.2';
18+
const version = '2.1.2';
1619
/**
1720
* calculates `P(n, k)`.
1821
*
1922
* @link https://en.wikipedia.org/wiki/Permutation
2023
*/
21-
export function permutation(n, k) {
24+
function permutation(n, k) {
2225
if (n < 0)
2326
throw new RangeError(`${n} is out of range`);
2427
if (k < 0)
@@ -37,7 +40,7 @@ export function permutation(n, k) {
3740
*
3841
* @link https://en.wikipedia.org/wiki/Combination
3942
*/
40-
export function combination(n, k) {
43+
function combination(n, k) {
4144
if (0 == k)
4245
return 1n;
4346
if (n == k)
@@ -51,7 +54,7 @@ export function combination(n, k) {
5154
*
5255
* @link https://en.wikipedia.org/wiki/Factorial
5356
*/
54-
export function factorial(n) {
57+
function factorial(n) {
5558
return permutation(n, n);
5659
}
5760
/**
@@ -60,7 +63,7 @@ export function factorial(n) {
6063
* @link https://en.wikipedia.org/wiki/Factorial_number_system
6164
* @param {number} l the number of digits
6265
*/
63-
export function factoradic(n, l = 0) {
66+
function factoradic(n, l = 0) {
6467
if (n < 0)
6568
throw new RangeError(`${n} is out of range`);
6669
let [bn, bf] = [BigInt(n), 1n];
@@ -87,7 +90,7 @@ export function factoradic(n, l = 0) {
8790
*
8891
* @link https://en.wikipedia.org/wiki/Combinatorial_number_system
8992
*/
90-
export function combinadic(n, k) {
93+
function combinadic(n, k) {
9194
const count = combination(n, k);
9295
const [bn, bk] = [BigInt(n), BigInt(k)];
9396
return (m) => {
@@ -124,7 +127,7 @@ const _randomBytes = typeof _crypto['randomBytes'] === 'function'
124127
* @param {anyint} min
125128
* @param {anyint} max
126129
*/
127-
export function randomInteger(min = 0, max = Math.pow(2, 53)) {
130+
function randomInteger(min = 0, max = Math.pow(2, 53)) {
128131
let ctor = min.constructor;
129132
if (arguments.length === 0) {
130133
return Math.floor(Math.random() * ctor(max));
@@ -142,7 +145,6 @@ export function randomInteger(min = 0, max = Math.pow(2, 53)) {
142145
const rnd = u8s.reduce((a, v) => ((a << ctor(8)) + ctor(v)), ctor(0));
143146
return ((ctor(rnd) * mag) >> ctor(len * 8)) + ctor(min);
144147
}
145-
;
146148
/**
147149
* Base Class of `js-combinatorics`
148150
*/
@@ -218,18 +220,6 @@ class _CBase {
218220
* an alias of `at`
219221
*/
220222
nth(n) { return this.at(n); }
221-
/**
222-
* the seed iterable
223-
*/
224-
seed;
225-
/**
226-
* the size (# of elements) of each element.
227-
*/
228-
size;
229-
/**
230-
* the number of elements
231-
*/
232-
length;
233223
/**
234224
* pick random element
235225
*/
@@ -249,7 +239,7 @@ class _CBase {
249239
/**
250240
* Permutation
251241
*/
252-
export class Permutation extends _CBase {
242+
class Permutation extends _CBase {
253243
constructor(seed, size = 0) {
254244
super();
255245
this.seed = [...seed];
@@ -275,8 +265,7 @@ export class Permutation extends _CBase {
275265
/**
276266
* Combination
277267
*/
278-
export class Combination extends _CBase {
279-
comb;
268+
class Combination extends _CBase {
280269
constructor(seed, size = 0) {
281270
super();
282271
this.seed = [...seed];
@@ -329,8 +318,7 @@ export class Combination extends _CBase {
329318
/**
330319
* Base N
331320
*/
332-
export class BaseN extends _CBase {
333-
base;
321+
class BaseN extends _CBase {
334322
constructor(seed, size = 1) {
335323
if (size < 1)
336324
throw new RangeError(`${size} is out of range`);
@@ -361,7 +349,7 @@ export class BaseN extends _CBase {
361349
/**
362350
* Power Set
363351
*/
364-
export class PowerSet extends _CBase {
352+
class PowerSet extends _CBase {
365353
constructor(seed) {
366354
super();
367355
this.seed = [...seed];
@@ -384,7 +372,7 @@ export class PowerSet extends _CBase {
384372
/**
385373
* Cartesian Product
386374
*/
387-
export class CartesianProduct extends _CBase {
375+
class CartesianProduct extends _CBase {
388376
constructor(...args) {
389377
super();
390378
this.seed = args.map(v => [...v]);
@@ -410,3 +398,5 @@ export class CartesianProduct extends _CBase {
410398
return result;
411399
}
412400
}
401+
402+
export { BaseN, CartesianProduct, Combination, Permutation, PowerSet, combinadic, combination, factoradic, factorial, permutation, randomInteger, version };

combinatorics.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* @link: http://en.wikipedia.org/wiki/Factorial_number_system
1313
* @link: https://en.wikipedia.org/wiki/Combinatorial_number_system
1414
*/
15-
export const version = '2.1.2';
15+
export const version: string = '__VERSION__';
1616
/**
1717
* BigInt Workaround
1818
*

commonjs/combinatorics.js

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
"use strict";
2-
Object.defineProperty(exports, "__esModule", { value: true });
3-
exports.CartesianProduct = exports.PowerSet = exports.BaseN = exports.Combination = exports.Permutation = exports.version = void 0;
4-
exports.permutation = permutation;
5-
exports.combination = combination;
6-
exports.factorial = factorial;
7-
exports.factoradic = factoradic;
8-
exports.combinadic = combinadic;
9-
exports.randomInteger = randomInteger;
1+
//
2+
// THIS FILE IS AUTOMATICALLY GENERATED! DO NOT EDIT BY HAND!
3+
//
4+
'use strict';
5+
106
/**
117
* combinatorics.js
128
*
@@ -21,7 +17,7 @@ exports.randomInteger = randomInteger;
2117
* @link: http://en.wikipedia.org/wiki/Factorial_number_system
2218
* @link: https://en.wikipedia.org/wiki/Combinatorial_number_system
2319
*/
24-
exports.version = '2.1.2';
20+
const version = '2.1.2';
2521
/**
2622
* calculates `P(n, k)`.
2723
*
@@ -151,7 +147,6 @@ function randomInteger(min = 0, max = Math.pow(2, 53)) {
151147
const rnd = u8s.reduce((a, v) => ((a << ctor(8)) + ctor(v)), ctor(0));
152148
return ((ctor(rnd) * mag) >> ctor(len * 8)) + ctor(min);
153149
}
154-
;
155150
/**
156151
* Base Class of `js-combinatorics`
157152
*/
@@ -269,7 +264,6 @@ class Permutation extends _CBase {
269264
return result;
270265
}
271266
}
272-
exports.Permutation = Permutation;
273267
/**
274268
* Combination
275269
*/
@@ -323,7 +317,6 @@ class Combination extends _CBase {
323317
return result;
324318
}
325319
}
326-
exports.Combination = Combination;
327320
/**
328321
* Base N
329322
*/
@@ -355,7 +348,6 @@ class BaseN extends _CBase {
355348
return result;
356349
}
357350
}
358-
exports.BaseN = BaseN;
359351
/**
360352
* Power Set
361353
*/
@@ -379,7 +371,6 @@ class PowerSet extends _CBase {
379371
return result;
380372
}
381373
}
382-
exports.PowerSet = PowerSet;
383374
/**
384375
* Cartesian Product
385376
*/
@@ -409,4 +400,16 @@ class CartesianProduct extends _CBase {
409400
return result;
410401
}
411402
}
403+
404+
exports.BaseN = BaseN;
412405
exports.CartesianProduct = CartesianProduct;
406+
exports.Combination = Combination;
407+
exports.Permutation = Permutation;
408+
exports.PowerSet = PowerSet;
409+
exports.combinadic = combinadic;
410+
exports.combination = combination;
411+
exports.factoradic = factoradic;
412+
exports.factorial = factorial;
413+
exports.permutation = permutation;
414+
exports.randomInteger = randomInteger;
415+
exports.version = version;

package.json

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,34 @@
55
"main": "combinatorics.js",
66
"module": "combinatorics.js",
77
"type": "module",
8-
"exports": {
9-
"import": "./combinatorics.js",
10-
"require": "./commonjs/combinatorics.js"
11-
},
128
"types": "combinatorics.d.ts",
9+
"sideEffects": false,
1310
"files": [
1411
"combinatorics.d.ts",
1512
"combinatorics.js",
1613
"umd",
1714
"commonjs"
1815
],
19-
"runkitExample": "require=require(\"esm\")(module);\nvar Combinatorics=require(\"js-combinatorics\");\n",
16+
"exports": {
17+
"import": "./combinatorics.js",
18+
"require": "./commonjs/combinatorics.js"
19+
},
20+
"scripts": {
21+
"build": "rollup -c && tsc -p . --declaration --emitDeclarationOnly",
22+
"clean": "node -e \"['combinatorics.js','combinatorics.d.ts','umd/combinatorics.js','commonjs/combinatorics.js'].forEach(f=>require('fs').rmSync(f,{force:true}))\"",
23+
"test": "npm run build && mocha",
24+
"version": "npm run build && node util/sync-version.js && git add combinatorics.js combinatorics.d.ts umd/combinatorics.js commonjs/combinatorics.js README.md"
25+
},
26+
"runkitExample": "var Combinatorics=require(\"js-combinatorics\");\n",
2027
"devDependencies": {
21-
"typescript": "^5.0.0",
28+
"@rollup/plugin-replace": "^6.0.3",
29+
"@rollup/plugin-typescript": "^12.3.0",
2230
"@types/node": "^20.9.0",
31+
"chai": "^4.2.0",
2332
"mocha": "^10.0.0",
24-
"chai": "^4.2.0"
25-
},
26-
"scripts": {
27-
"test": "make clean && make test"
33+
"rollup": "^4.62.3",
34+
"tslib": "^2.8.1",
35+
"typescript": "^5.0.0"
2836
},
2937
"repository": {
3038
"type": "git",
@@ -37,6 +45,5 @@
3745
],
3846
"author": "Dan Kogai",
3947
"license": "MIT",
40-
"readmeFilename": "README.md",
41-
"gitHead": "e3684eb54f7c6fbb24bb7a4d08d88671ce12e9eb"
48+
"readmeFilename": "README.md"
4249
}

0 commit comments

Comments
 (0)