-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is the async counterpart to mapContextSync. Given an async iterable of MessageContext instance and an array of ids (or a single id), it maps each identifier to the first MessageContext which contains the message for it. An ordered interable of MessageContext instances can represent the current negotiated fallback chain of languages. This iterable can be used to find the best existing translation for a given identifier. The iterable of MessageContexts can now be async, allowing code like this: async formatString(id, args) { const ctx = await mapContextAsync(contexts, id); if (ctx === null) { return id; } const msg = ctx.getMessage(id); return ctx.format(msg, args); } The iterable of MessageContexts should always be wrapped in CachedIterable to optimize subsequent calls to mapContextSync and mapContextAsync.
- Loading branch information
Showing
5 changed files
with
124 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import assert from 'assert'; | ||
|
||
import CachedIterable from '../src/cached_iterable'; | ||
import MessageContext from './message_context_stub'; | ||
import { mapContextAsync } from '../src/index'; | ||
|
||
suite('Async Fallback — single id', function() { | ||
let ctx1, ctx2; | ||
|
||
suiteSetup(function() { | ||
ctx1 = new MessageContext(); | ||
ctx1._setMessages(['bar']); | ||
ctx2 = new MessageContext(); | ||
ctx2._setMessages(['foo', 'bar']); | ||
}); | ||
|
||
test('eager iterable', async function() { | ||
const contexts = new CachedIterable([ctx1, ctx2]); | ||
assert.equal(await mapContextAsync(contexts, 'foo'), ctx2); | ||
assert.equal(await mapContextAsync(contexts, 'bar'), ctx1); | ||
}); | ||
|
||
test('eager iterable works more than once', async function() { | ||
const contexts = new CachedIterable([ctx1, ctx2]); | ||
assert.equal(await mapContextAsync(contexts, 'foo'), ctx2); | ||
assert.equal(await mapContextAsync(contexts, 'bar'), ctx1); | ||
assert.equal(await mapContextAsync(contexts, 'foo'), ctx2); | ||
assert.equal(await mapContextAsync(contexts, 'bar'), ctx1); | ||
}); | ||
|
||
test('lazy iterable', async function() { | ||
async function *generateMessages() { | ||
yield *[ctx1, ctx2]; | ||
} | ||
|
||
const contexts = new CachedIterable(generateMessages()); | ||
assert.equal(await mapContextAsync(contexts, 'foo'), ctx2); | ||
assert.equal(await mapContextAsync(contexts, 'bar'), ctx1); | ||
}); | ||
|
||
test('lazy iterable works more than once', async function() { | ||
async function *generateMessages() { | ||
yield *[ctx1, ctx2]; | ||
} | ||
|
||
const contexts = new CachedIterable(generateMessages()); | ||
assert.equal(await mapContextAsync(contexts, 'foo'), ctx2); | ||
assert.equal(await mapContextAsync(contexts, 'bar'), ctx1); | ||
assert.equal(await mapContextAsync(contexts, 'foo'), ctx2); | ||
assert.equal(await mapContextAsync(contexts, 'bar'), ctx1); | ||
}); | ||
}); | ||
|
||
suite('Async Fallback — multiple ids', async function() { | ||
let ctx1, ctx2; | ||
|
||
suiteSetup(function() { | ||
ctx1 = new MessageContext(); | ||
ctx1._setMessages(['foo', 'bar']); | ||
ctx2 = new MessageContext(); | ||
ctx2._setMessages(['foo', 'bar', 'baz']); | ||
}); | ||
|
||
test('existing translations', async function() { | ||
const contexts = new CachedIterable([ctx1, ctx2]); | ||
assert.deepEqual( | ||
await mapContextAsync(contexts, ['foo', 'bar']), | ||
[ctx1, ctx1] | ||
); | ||
}); | ||
|
||
test('fallback translations', async function() { | ||
const contexts = new CachedIterable([ctx1, ctx2]); | ||
assert.deepEqual( | ||
await mapContextAsync(contexts, ['foo', 'bar', 'baz']), | ||
[ctx1, ctx1, ctx2] | ||
); | ||
}); | ||
|
||
test('missing translations', async function() { | ||
const contexts = new CachedIterable([ctx1, ctx2]); | ||
assert.deepEqual( | ||
await mapContextAsync(contexts, ['foo', 'bar', 'baz', 'qux']), | ||
[ctx1, ctx1, ctx2, null] | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters