Skip to content

Commit 0b3a389

Browse files
authored
Merge pull request #450 from stasm/ts-sequence
Migrate @fluent/sequence to TypeScript
2 parents edddd02 + baaddaa commit 0b3a389

18 files changed

+187
-144
lines changed

fluent-bundle/eslint_src.json renamed to eslint_ts.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"plugin:@typescript-eslint/eslint-recommended",
1414
"plugin:@typescript-eslint/recommended",
1515
"plugin:@typescript-eslint/recommended-requiring-type-checking",
16-
"../eslint_src.json"
16+
"./eslint_src.json"
1717
],
1818
"rules": {
1919
"prefer-const": "off",

fluent-bundle/.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
esm/
2-
index.js
3-
compat.js
2+
/index.js
3+
/compat.js

fluent-bundle/makefile

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ GLOBAL := FluentBundle
44
include ../common.mk
55

66
lint:
7-
@eslint --config ./eslint_src.json --max-warnings 0 src/*.ts
7+
@eslint --config $(ROOT)/eslint_ts.json --max-warnings 0 src/*.ts
88
@eslint --config $(ROOT)/eslint_test.json --max-warnings 0 test/
99
@echo -e " $(OK) lint"
1010

@@ -16,6 +16,13 @@ esm/.compiled: $(SOURCES)
1616
@touch $@
1717
@echo -e " $(OK) esm/ compiled"
1818

19+
.PHONY: test
20+
test: esm/.compiled
21+
@nyc --reporter=text --reporter=html mocha \
22+
--recursive --ui tdd \
23+
--require esm \
24+
test/**/*_test.js
25+
1926
build: index.js compat.js
2027

2128
index.js: esm/.compiled
@@ -36,13 +43,6 @@ compat.js: esm/.compiled
3643
--output.file $@
3744
@echo -e " $(OK) $@ built"
3845

39-
.PHONY: test
40-
test: esm/.compiled
41-
@nyc --reporter=text --reporter=html mocha \
42-
--recursive --ui tdd \
43-
--require esm \
44-
test/**/*_test.js
45-
4646
html:
4747
@typedoc src \
4848
--out ../html/bundle \

fluent-sequence/.esdoc.json

Lines changed: 0 additions & 16 deletions
This file was deleted.

fluent-sequence/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
esm/
12
/index.js
23
/compat.js

fluent-sequence/.npmignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
.nyc_output
22
coverage
3-
docs
3+
esm/.compiled
4+
src
45
test
56
makefile
7+
tsconfig.json

fluent-sequence/makefile

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,21 @@ GLOBAL := FluentSequence
33

44
include ../common.mk
55

6-
test:
6+
lint:
7+
@eslint --config $(ROOT)/eslint_ts.json --max-warnings 0 src/*.ts
8+
@eslint --config $(ROOT)/eslint_test.json --max-warnings 0 test/
9+
@echo -e " $(OK) lint"
10+
11+
.PHONY: compile
12+
compile: esm/.compiled
13+
14+
esm/.compiled: $(SOURCES)
15+
@tsc
16+
@touch $@
17+
@echo -e " $(OK) esm/ compiled"
18+
19+
.PHONY: test
20+
test: esm/.compiled
721
@nyc --reporter=text --reporter=html mocha \
822
--recursive --ui tdd \
923
--require esm \
@@ -12,7 +26,7 @@ test:
1226
build: index.js compat.js
1327

1428
index.js: $(SOURCES)
15-
@rollup $(CURDIR)/src/index.js \
29+
@rollup $(CURDIR)/esm/index.js \
1630
--config $(ROOT)/bundle_config.js \
1731
--banner "/* $(PACKAGE)@$(VERSION) */" \
1832
--amd.id $(PACKAGE) \
@@ -21,14 +35,26 @@ index.js: $(SOURCES)
2135
@echo -e " $(OK) $@ built"
2236

2337
compat.js: $(SOURCES)
24-
@rollup $(CURDIR)/src/index.js \
38+
@rollup $(CURDIR)/esm/index.js \
2539
--config $(ROOT)/compat_config.js \
2640
--banner "/* $(PACKAGE)@$(VERSION) */" \
2741
--amd.id $(PACKAGE) \
2842
--name $(GLOBAL) \
2943
--output.file $@
3044
@echo -e " $(OK) $@ built"
3145

32-
lint: _lint
33-
html: _html
34-
clean: _clean
46+
html:
47+
@typedoc src \
48+
--out ../html/bundle \
49+
--mode file \
50+
--excludeNotExported \
51+
--excludePrivate \
52+
--logger none \
53+
--hideGenerator
54+
@echo -e " $(OK) html build"
55+
56+
clean:
57+
@rm -f esm/*.js esm/*.d.ts esm/.compiled
58+
@rm -f index.js compat.js
59+
@rm -rf .nyc_output coverage
60+
@echo -e " $(OK) clean"

fluent-sequence/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@
1515
"email": "[email protected]"
1616
}
1717
],
18-
"directories": {
19-
"lib": "./src"
20-
},
18+
"type": "commonjs",
2119
"main": "./index.js",
22-
"module": "./src/index.js",
20+
"module": "./esm/index.js",
21+
"types": "./esm/index.d.ts",
2322
"repository": {
2423
"type": "git",
2524
"url": "https://github.com/projectfluent/fluent.js.git"
@@ -32,6 +31,7 @@
3231
"node": ">=10.0.0"
3332
},
3433
"devDependencies": {
34+
"@fluent/bundle": ">= 0.15.0",
3535
"cached-iterable": "^0.2.1"
3636
},
3737
"peerDependencies": {

fluent-sequence/src/index.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

fluent-sequence/src/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/*
2+
* @module fluent-sequence
3+
* @overview Manage ordered sequences of FluentBundles.
4+
*/
5+
6+
export { mapBundleSync } from "./map_sync.js";
7+
export { mapBundleAsync } from "./map_async.js";

fluent-sequence/src/map_async.js

Lines changed: 0 additions & 36 deletions
This file was deleted.

fluent-sequence/src/map_async.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { FluentBundle } from "@fluent/bundle";
2+
3+
export function mapBundleAsync(
4+
bundles: AsyncIterable<FluentBundle>,
5+
ids: string
6+
): Promise<FluentBundle | null>
7+
8+
export function mapBundleAsync(
9+
bundles: AsyncIterable<FluentBundle>,
10+
ids: Array<string>
11+
): Promise<Array<FluentBundle | null>>;
12+
13+
/*
14+
* Asynchronously map an identifier or an array of identifiers to the best
15+
* `FluentBundle` instance(s).
16+
*
17+
* @param bundles - An iterable of bundles to sift through.
18+
* @param ids - An id or ids to map.
19+
*/
20+
export async function mapBundleAsync(
21+
bundles: AsyncIterable<FluentBundle>,
22+
ids: string | Array<string>
23+
): Promise<FluentBundle | null | Array<FluentBundle | null>> {
24+
if (!Array.isArray(ids)) {
25+
for await (const bundle of bundles) {
26+
if (bundle.hasMessage(ids)) {
27+
return bundle;
28+
}
29+
}
30+
31+
return null;
32+
}
33+
34+
const foundBundles = new Array(ids.length).fill(null);
35+
let remainingCount = ids.length;
36+
37+
for await (const bundle of bundles) {
38+
for (const [index, id] of ids.entries()) {
39+
if (!foundBundles[index] && bundle.hasMessage(id)) {
40+
foundBundles[index] = bundle;
41+
remainingCount--;
42+
}
43+
44+
// Return early when all ids have been mapped to bundles.
45+
if (remainingCount === 0) {
46+
return foundBundles;
47+
}
48+
}
49+
}
50+
51+
return foundBundles;
52+
}

fluent-sequence/src/map_sync.js

Lines changed: 0 additions & 30 deletions
This file was deleted.

fluent-sequence/src/map_sync.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { FluentBundle } from "@fluent/bundle";
2+
3+
export function mapBundleSync(
4+
bundles: Iterable<FluentBundle>,
5+
ids: string
6+
): FluentBundle | null;
7+
8+
export function mapBundleSync(
9+
bundles: Iterable<FluentBundle>,
10+
ids: Array<string>
11+
): Array<FluentBundle | null>;
12+
13+
/*
14+
* Synchronously map an identifier or an array of identifiers to the best
15+
* `FluentBundle` instance(s).
16+
*
17+
* @param bundles - An iterable of bundles to sift through.
18+
* @param ids - An id or ids to map.
19+
*/
20+
export function mapBundleSync(
21+
bundles: Iterable<FluentBundle>,
22+
ids: string | Array<string>
23+
): FluentBundle | null | Array<FluentBundle | null> {
24+
if (!Array.isArray(ids)) {
25+
return getBundleForId(bundles, ids);
26+
}
27+
28+
return ids.map(
29+
id => getBundleForId(bundles, id)
30+
);
31+
}
32+
33+
/*
34+
* Find the best `FluentBundle` with the translation for `id`.
35+
*/
36+
function getBundleForId(
37+
bundles: Iterable<FluentBundle>,
38+
id: string
39+
): NonNullable<FluentBundle> | null {
40+
for (const bundle of bundles) {
41+
if (bundle.hasMessage(id)) {
42+
return bundle;
43+
}
44+
}
45+
46+
return null;
47+
}

fluent-sequence/test/bundle_stub.js

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)