• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

TypeScript multimethods.create函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了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;
    });
开发者ID:yortus,项目名称:multimethods,代码行数:15,代码来源:multimethod.ts


示例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;
    });
开发者ID:yortus,项目名称:multimethods,代码行数:19,代码来源:multimethod.ts


示例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`,
//.........这里部分代码省略.........
开发者ID:yortus,项目名称:multimethods,代码行数:101,代码来源:constructing-a-multimethod-instance.ts


示例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,
});
开发者ID:yortus,项目名称:multimethods,代码行数:40,代码来源:basic-routing.ts



注:本文中的multimethods.create函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
TypeScript multiparty.Form类代码示例发布时间:2022-05-25
下一篇:
TypeScript multer-gridfs-storage.removeAllListeners函数代码示例发布时间:2022-05-25
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap