Skip to content

Commit 0f95f63

Browse files
committed
(@fluent/sequence) Migrate to TypeScript
1 parent edddd02 commit 0f95f63

16 files changed

+151
-83
lines changed

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: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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/eslint_src.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"parser": "@typescript-eslint/parser",
3+
"parserOptions": {
4+
"ecmaVersion": 9,
5+
"sourceType": "module",
6+
"project": "./tsconfig.json"
7+
},
8+
"env": {
9+
"es6": true
10+
},
11+
"extends": [
12+
"eslint:recommended",
13+
"plugin:@typescript-eslint/eslint-recommended",
14+
"plugin:@typescript-eslint/recommended",
15+
"plugin:@typescript-eslint/recommended-requiring-type-checking",
16+
"../eslint_src.json"
17+
],
18+
"rules": {
19+
"prefer-const": "off",
20+
"no-unused-vars": ["error", {"args": "none"}],
21+
"@typescript-eslint/no-inferrable-types": "off",
22+
"@typescript-eslint/no-unused-vars": ["error", {"args": "none"}],
23+
"@typescript-eslint/no-use-before-define": "off",
24+
"@typescript-eslint/explicit-function-return-type": "error"
25+
}
26+
}

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 ./eslint_src.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 renamed to fluent-sequence/src/map_async.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,37 @@
1+
import { FluentBundle } from "@fluent/bundle";
2+
13
/*
24
* Asynchronously map an identifier or an array of identifiers to the best
35
* `FluentBundle` instance(s).
46
*
5-
* @param {AsyncIterable} iterable
6-
* @param {string|Array<string>} ids
7-
* @returns {Promise<FluentBundle|Array<FluentBundle>>}
7+
* @param bundles - An iterable of bundles to sift through.
8+
* @param ids - An id or ids to map.
89
*/
9-
export default async function mapBundleAsync(iterable, ids) {
10+
export async function mapBundleAsync(
11+
bundles: AsyncIterable<FluentBundle>,
12+
ids: string | Array<string>
13+
): Promise<FluentBundle | null | Array<FluentBundle | null>> {
1014
if (!Array.isArray(ids)) {
11-
for await (const bundle of iterable) {
15+
for await (const bundle of bundles) {
1216
if (bundle.hasMessage(ids)) {
1317
return bundle;
1418
}
1519
}
20+
21+
return null;
1622
}
1723

24+
const foundBundles = new Array(ids.length).fill(null);
1825
let remainingCount = ids.length;
19-
const foundBundles = new Array(remainingCount).fill(null);
2026

21-
for await (const bundle of iterable) {
27+
for await (const bundle of bundles) {
2228
for (const [index, id] of ids.entries()) {
2329
if (!foundBundles[index] && bundle.hasMessage(id)) {
2430
foundBundles[index] = bundle;
2531
remainingCount--;
2632
}
2733

28-
// Return early when all ids have been mapped to contexts.
34+
// Return early when all ids have been mapped to bundles.
2935
if (remainingCount === 0) {
3036
return foundBundles;
3137
}

fluent-sequence/src/map_sync.js

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

fluent-sequence/src/map_sync.ts

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

fluent-sequence/test/fallback_async_test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import assert from 'assert';
22

33
import {CachedAsyncIterable} from 'cached-iterable';
44
import FluentBundle from './bundle_stub';
5-
import {mapBundleAsync} from '../src/index';
5+
import {mapBundleAsync} from '../esm/index';
66

77
suite('Async Fallback — single id', function() {
88
let bundle1, bundle2, generateBundles;

fluent-sequence/test/fallback_sync_test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import assert from 'assert';
22

33
import {CachedSyncIterable} from 'cached-iterable';
44
import FluentBundle from './bundle_stub';
5-
import {mapBundleSync} from '../src/index';
5+
import {mapBundleSync} from '../esm/index';
66

77
suite('Sync Fallback — single id', function() {
88
let bundle1, bundle2;

fluent-sequence/tsconfig.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es2019",
4+
"module": "es2015",
5+
"strict": true,
6+
"allowJs": false,
7+
"esModuleInterop": true,
8+
"moduleResolution": "node",
9+
"noEmitHelpers": true,
10+
"declaration": true,
11+
"outDir": "./esm",
12+
},
13+
"include": [
14+
"./src/**/*.ts"
15+
]
16+
}

0 commit comments

Comments
 (0)