本文整理匯總了TypeScript中multimethods.create函數的典型用法代碼示例。如果您正苦於以下問題:TypeScript create函數的具體用法?TypeScript create怎麽用?TypeScript create使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了create函數的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: it
it('TEMP1', () => {
let mm = MM({
arity: 1,
toDiscriminant: (x: string) => x,
methods: {
'/{thing}': (x, {}, _) => x,
'/foo': (x) => 'foo' + x,
'/bar': (x) => 'bar' + x,
'**': meta((x, _, next) => `---${next(x)}---`),
},
});
let result = mm('/foo');
result = result;
});
示例2: expect
it('TEMP4', async () => {
let mm = MM({
arity: 2,
async: true,
strict: true,
methods: {
'**': (a: any, b: any) => Promise.resolve(`${a}:${b}`),
'/String**': (_: string, __: any) => Promise.resolve(`first is string`),
'/Number**': (_: number, __: any) => { throw new Error('oops'); },
'/Number/Boolean': (_: number, __: any) => `num:bool`,
},
});
await expect(mm('foo', 42)).to.eventually.equal('first is string');
await expect(mm(true, 42)).to.eventually.equal('true:42');
await expect(mm(42, 'foo')).to.be.rejected;
await expect(mm(42, true)).to.be.rejected;
});
示例3: calc
variants.forEach(({vname, async, val, err}) => describe(`(${vname})`, () => {
let methods = {
'/**': () => err('nothing matches!'),
'/foo': () => val('foo'),
'/bar': () => val('bar'),
'/baz': () => val('baz'),
'/*a*': meta((rq, _, next) => {
return calc([
'---',
calc(next(rq), rs => rs === CONTINUE ? err('no downstream!') : rs),
'---',
], concat);
}),
'a/*': () => val(`starts with 'a'`),
'*/b': () => val(`ends with 'b'`),
'a/b': () => val(`starts with 'a' AND ends with 'b'`),
'c/*': () => val(`starts with 'c'`),
'*/d': () => err(`don't end with 'd'!`),
'c/d': () => val(CONTINUE),
'api/**': () => val(`fallback`),
'api/fo*o': () => val(CONTINUE),
'api/fo*': [
meta((rq, _, next) => {
return calc(['fo2-(', calc(next(rq), rs => rs === CONTINUE ? val('NONE') : rs), ')'], concat);
}),
meta((rq, _, next) => {
return calc(['fo1-(', calc(next(rq), rs => rs === CONTINUE ? val('NONE') : rs), ')'], concat);
}),
],
'api/foo': [
meta((rq, _, next) => calc([calc(next(rq), rs => rs === CONTINUE ? val('NONE') : rs), '!'], concat)),
() => val('FOO'),
],
'api/foot': rq => val(`FOOt${rq.address.length}`),
'api/fooo': () => val('fooo'),
'api/bar': () => val(CONTINUE),
'zz/z/{**rest}': meta((_, {rest}, next) => {
let moddedReq = {address: rest.split('').reverse().join('')};
return calc(next(moddedReq), rs => rs === CONTINUE ? val('NONE') : rs);
}),
'zz/z/b*z': (rq) => val(`${rq.address}`),
'zz/z/./*': () => val('forty-two'),
'CHAIN-{x}': [
// Wrap subsequent results with ()
meta((rq, {}, next) => calc(['(', next(rq), ')'], concat)),
// Block any result that starts with '[32'
meta((rq, {}, next) => calc(next(rq), rs => rs.startsWith('[32') ? err('blocked') : rs)),
// Wrap subsequent results with []
meta((rq, {}, next) => calc(['[', next(rq), ']'], concat)),
// Return x!x! only if x ends with 'b' , otherwise skip
(_, {x}) => val(x.endsWith('b') ? (x + '!').repeat(2) : CONTINUE),
// Return xxx only if x has length 2, otherwise skip
(_, {x}) => val(x.length === 2 ? x.repeat(3) : CONTINUE),
// Return the string reversed
(_, {x}) => val(x.split('').reverse().join('')),
],
};
let tests = [
`/foo ==> foo`,
`/bar ==> ---bar---`,
`/baz ==> ---baz---`,
`/quux ==> ERROR: nothing matches!`,
`quux ==> ERROR: Multimethod dispatch failure...`,
`/qaax ==> ERROR: no downstream!`,
`/a ==> ERROR: no downstream!`,
`a ==> ERROR: Multimethod dispatch failure...`,
`/ ==> ERROR: nothing matches!`,
` ==> ERROR: Multimethod dispatch failure...`,
`a/foo ==> starts with 'a'`,
`foo/b ==> ends with 'b'`,
`a/b ==> starts with 'a' AND ends with 'b'`,
`c/foo ==> starts with 'c'`,
`foo/d ==> ERROR: don't end with 'd'!`,
`c/d ==> ERROR: Multiple possible fallbacks...`,
`api/ ==> fallback`,
`api/foo ==> fo2-(fo1-(FOO!))`,
`api/fooo ==> fo2-(fo1-(fooo))`,
`api/foooo ==> fo2-(fo1-(NONE))`,
`api/foooot ==> fo2-(fo1-(NONE))`,
`api/foot ==> fo2-(fo1-(FOOt8))`,
`api/bar ==> fallback`,
`zz/z/baz ==> zab`,
`zz/z/booz ==> zoob`,
`zz/z/looz ==> NONE`,
//.........這裏部分代碼省略.........
示例4: MM
const mm = MM({
toDiscriminant: (r: {address: string}) => r.address,
methods: {
'**': () => 'UNHANDLED',
'/foo': () => 'foo',
'/bar': () => 'bar',
'/baz': () => 'baz',
'/*a*': meta(($req, _, next) => `---${ifUnhandled(next($req), 'NONE')}---`),
'a/*': () => `starts with 'a'`,
'*/b': () => `ends with 'b'`,
'a/b': () => `starts with 'a' AND ends with 'b'`,
'c/*': () => `starts with 'c'`,
'*/d': () => `ends with 'd'`,
'c/d': () => CONTINUE,
'api/**': [() => `fallback`, () => `fallback`],
'api/fo*o': () => CONTINUE,
'api/fo*': [
meta(($req, _, next) => `fo2-(${ifUnhandled(next($req), 'NONE')})`),
meta(($req, _, next) => `fo1-(${ifUnhandled(next($req), 'NONE')})`),
],
'api/foo': [
meta(($req, _, next) => `${ifUnhandled(next($req), 'NONE')}!`),
() => 'FOO',
],
'api/foot': () => 'FOOt',
'api/fooo': () => 'fooo',
'api/bar': () => CONTINUE,
// NB: V8 profiling shows the native string functions show up heavy in the perf profile (i.e. more than MM infrastructure!)
'zz/z/{**rest}': meta((_, {rest}, next) => `${ifUnhandled(next({address: rest.split('').reverse().join('')}), 'NONE')}`),
'zz/z/b*z': ($req) => `${$req.address}`,
'zz/z/./*': () => 'forty-two',
},
arity: 1,
async: false,
strict: false,
});