Skip to content
Snippets Groups Projects
Select Git revision
  • 40aa847d1f9c5d2a1a5161ecedb879c2657f9416
  • master default protected
  • rednatco-v2
  • base-pairs-ladder
  • rednatco
  • test
  • ntc-tube-uniform-color
  • ntc-tube-missing-atoms
  • restore-vertex-array-per-program
  • watlas2
  • dnatco_new
  • cleanup-old-nodejs
  • webmmb
  • fix_auth_seq_id
  • update_deps
  • ext_dev
  • ntc_balls
  • nci-2
  • plugin
  • bugfix-0.4.5
  • nci
  • v0.5.0-dev.1
  • v0.4.5
  • v0.4.4
  • v0.4.3
  • v0.4.2
  • v0.4.1
  • v0.4.0
  • v0.3.12
  • v0.3.11
  • v0.3.10
  • v0.3.9
  • v0.3.8
  • v0.3.7
  • v0.3.6
  • v0.3.5
  • v0.3.4
  • v0.3.3
  • v0.3.2
  • v0.3.1
  • v0.3.0
41 results

symbol.ts

Blame
  • symbol.ts 3.36 KiB
    /*
     * Copyright (c) 2018 Mol* contributors, licensed under MIT, See LICENSE file for more info.
     *
     * @author David Sehnal <david.sehnal@gmail.com>
     */
    
    import Type from './type'
    import Expression from './expression'
    
    export type Argument<T extends Type = Type>  = {
        type: T,
        isOptional: boolean,
        isRest: boolean,
        defaultValue: T['@type'] | undefined,
        description: string | undefined
    }
    export function Argument<T extends Type>(type: T, params?: { description?: string, defaultValue?: T['@type'], isOptional?: boolean, isRest?: boolean }): Argument<T> {
        const { description = void 0, isOptional = false, isRest = false, defaultValue = void 0 } = params || {}
        return { type, isOptional, isRest, defaultValue, description };
    }
    
    export type Arguments<T extends { [key: string]: any } = {}> =
        | Arguments.List<T>
        | Arguments.Dictionary<T>
    
    export namespace Arguments {
        export const None: Arguments = Dictionary({});
    
        export interface Dictionary<T extends { [key: string]: any } = {}, Traits = {}> {
            kind: 'dictionary',
            map: { [P in keyof T]: Argument<T[P]> },
            '@type': T
        }
        export type PropTypes<Map extends { [key: string]: Argument<any>  }> = { [P in keyof Map]: Map[P]['type']['@type'] }
        export function Dictionary<Map extends { [key: string]: Argument<any> }>(map: Map): Arguments<PropTypes<Map>> {
            return { kind: 'dictionary', map, '@type': 0 as any };
        }
    
        export interface List<T extends { [key: string]: any } = {}, Traits = {}> {
            kind: 'list',
            type: Type,
            nonEmpty: boolean,
            '@type': T
        }
    
        export function List<T extends Type>(type: T, params?: { nonEmpty?: boolean }): Arguments<{ [key: string]: T['@type'] }> {
            const { nonEmpty = false } = params || { }
            return { kind: 'list', type, nonEmpty, '@type': 0 as any };
        }
    }
    
    export type ExpressionArguments<T> = { [P in keyof T]?: Expression } | { [index: number]: Expression }
    
    export interface MSymbol<A extends Arguments = Arguments, T extends Type = Type> {
        (args?: ExpressionArguments<A['@type']>): Expression,
        info: {
            namespace: string,
            name: string,
            description?: string
        },
        args: A
        type: T,
        id: string,
    }
    
    export function MSymbol<A extends Arguments, T extends Type>(name: string, args: A, type: T, description?: string) {
        const symbol: MSymbol<A, T> = function(args: ExpressionArguments<A['@type']>) {
            return Expression.Apply(Expression.Symbol(symbol.id), args as any);
        } as any;
        symbol.info = { namespace: '', name, description };