Skip to content
Snippets Groups Projects
Select Git revision
  • b5c5a431728f90d21adafeda1a2f9970337df3f2
  • master default protected
  • devel
  • hruska-feature-clients-api
  • malostik-#5066-deduplicate-idea-ids
  • warden-postgresql-port
  • hruska-feature-#6799-filter-keys
  • hruska-feature-5066-duplicateIdeaID
  • warden-client-3.0-beta3
  • warden-server-3.0-beta3
  • warden-client-2.2-final
  • warden-server-2.2-final
  • warden-client-3.0-beta2
  • warden-server-3.0-beta2
  • warden-client-2.2
  • warden-server-2.2-patch3
  • warden-client-3.0-beta1
  • warden-server-3.0-beta1
  • warden-server-2.2-patch1
  • warden-client-3.0-beta0
  • warden-server-3.0-beta0
  • warden-server-2.2
  • warden-server-2.1-patch1
  • warden-client-2.1
  • warden-server-2.1
  • warden-server-2.1-beta6
  • warden-server-2.1-beta5
  • warden-server-2.1-beta4
28 results

WardenClientSend.pm

Blame
  • helpers.ts 1.76 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 { MSymbol, Arguments, isSymbol } from './symbol';
    
    export function symbol<A extends Arguments, T extends Type<S>, S>(args: A, type: T, description?: string) {
        return MSymbol('', args, type, description);
    }
    
    export function normalizeTable(table: any) {
        _normalizeTable('', '', table);
    }
    
    export function symbolList(table: any): MSymbol[] {
        const list: MSymbol[] = [];
        _symbolList(table, list);
        return list;
    }
    
    function formatKey(key: string) {
        const regex = /([a-z])([A-Z])([a-z]|$)/g;
        // do this twice because 'xXxX'
        return key.replace(regex, (s, a, b, c) => `${a}-${b.toLocaleLowerCase()}${c}`).replace(regex, (s, a, b, c) => `${a}-${b.toLocaleLowerCase()}${c}`);
    }
    
    function _normalizeTable(namespace: string, key: string, obj: any) {
        if (isSymbol(obj)) {
            obj.info.namespace = namespace;
            obj.info.name = obj.info.name || formatKey(key);
            obj.id = `${obj.info.namespace}.${obj.info.name}`;
            return;
        }
        const currentNs = `${obj['@namespace'] || formatKey(key)}`;
        const newNs = namespace ? `${namespace}.${currentNs}` : currentNs;
        for (const childKey of Object.keys(obj)) {
            if (typeof obj[childKey] !== 'object' && !isSymbol(obj[childKey])) continue;
            _normalizeTable(newNs, childKey, obj[childKey]);
        }
    }
    
    function _symbolList(obj: any, list: MSymbol[]) {
        if (isSymbol(obj)) {
            list.push(obj);
            return;
        }
        for (const childKey of Object.keys(obj)) {
            if (typeof obj[childKey] !== 'object' && !isSymbol(obj[childKey])) continue;
            _symbolList(obj[childKey], list);
        }
    }