How to use typical - 10 common examples

To help you get started, we’ve selected a few typical examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github 75lb / array-tools / dist / array-tools.js View on Github external
return negate(a.exists(propertyValue, queryValue), isNegated);

        /* query value is a regex */
        } else if (queryValue instanceof RegExp){
            if ([ "boolean", "string", "number" ].indexOf(typeof propertyValue)  === -1){
                return isNegated;
            } else {
                return negate(queryValue.test(propertyValue), isNegated);
            }

        /* query value is a function */
        } else if (typeof queryValue === "function"){
            return negate(queryValue(propertyValue), isNegated);

        /* query and property value are objects */
        } else if (t.isObject(propertyValue) && t.isObject(queryValue)){
            return negate(exists(propertyValue, queryValue), isNegated);

        /* query value is a primitive */
        } else {
            return negate(queryValue === propertyValue, isNegated);
        }
    });
}
github 75lb / table-layout / src / bin / cli.js View on Github external
.pipe(collectJson(function (json) {
    let clOptions = {
      maxWidth: process.stdout.columns,
      padding: {}
    }

    if (t.isDefined(options['padding-left'])) clOptions.padding.left = options['padding-left']
    if (t.isDefined(options['padding-right'])) clOptions.padding.right = options['padding-right']

    /* split input into data and options */
    if (!Array.isArray(json)) {
      if (json.options && json.data) {
        clOptions = extend(clOptions, json.options)
        json = json.data
      } else {
        throw new Error('Invalid input data')
      }
    }

    if (columns.length) clOptions.columns = columns

    const table = new tableLayout.Table(json, clOptions)
    return options.lines
      ? JSON.stringify(table.renderLines(), null, '  ') + '\n'
github 75lb / command-line-args / lib / argv-parser.js View on Github external
* [Symbol.iterator] () {
    const definitions = this.definitions
    const t = require('typical')

    let def
    let value
    let name
    let event
    let singularDefaultSet = false
    let unknownFound = false
    let origArg

    for (let arg of this.argv) {
      if (t.isPlainObject(arg)) {
        origArg = arg.origArg
        arg = arg.arg
      }

      if (unknownFound && this.options.stopAtFirstUnknown) {
        yield { event: 'unknown_value', arg, name: '_unknown', value: undefined }
        continue
      }

      /* handle long or short option */
      if (argvTools.isOption(arg)) {
        def = definitions.get(arg)
        value = undefined
        if (def) {
          value = def.isBoolean() ? true : null
          event = 'set'
github lwsjs / lws / lib / util.js View on Github external
function customiser (previousValue, newValue, key, object, source) {
    /* deep merge plain objects */
    if (t.isPlainObject(previousValue) && t.isPlainObject(newValue)) {
      return assignWith(previousValue, newValue, customiser)
    /* overwrite arrays if the new array has items */
    } else if (Array.isArray(previousValue) && Array.isArray(newValue) && newValue.length) {
      return newValue
    /* ignore incoming arrays if empty */
    } else if (Array.isArray(newValue) && !newValue.length) {
      return previousValue
    } else if (!t.isDefined(previousValue) && Array.isArray(newValue)) {
      return newValue
    }
  }
github 75lb / command-line-usage / lib / content.js View on Github external
lines () {
    const content = this._content
    const defaultPadding = { left: '  ', right: ' ' }

    if (content) {
      /* string content */
      if (t.isString(content)) {
        const table = new Table({ column: chalkFormat(content) }, {
          padding: defaultPadding,
          maxWidth: 80
        })
        return table.renderLines()

      /* array of strings */
      } else if (Array.isArray(content) && content.every(t.isString)) {
        const rows = content.map(string => ({ column: chalkFormat(string) }))
        const table = new Table(rows, {
          padding: defaultPadding,
          maxWidth: 80
        })
        return table.renderLines()

      /* array of objects (use table-layout) */
github lwsjs / local-web-server / lib / middleware.js View on Github external
let target = targets.find(target => {
        return testValue(target, {
          request: {
            method: [ ctx.method, undefined ],
            accepts: type => ctx.accepts(type)
          }
        })
      })

      /* else take the first target without a request (no request means 'all requests') */
      if (!target) {
        target = targets.find(target => !target.request)
      }

      if (target) {
        if (t.isFunction(target.response)) {
          const pathMatches = ctx.path.match(pathRe).slice(1)
          return target.response.apply(null, [ctx].concat(pathMatches))
        } else if (t.isPlainObject(target.response)) {
          Object.assign(ctx.response, target.response)
        } else {
          throw new Error(`Invalid response: ${JSON.stringify(target.response)}`)
        }
      }
    } else {
      return next()
    }
  }
}
github ipld / js-ipld / src / index.js View on Github external
putMany (nodes, format, userOptions) {
    if (!typical.isIterable(nodes) || typeof nodes === 'string' ||
        Buffer.isBuffer(nodes)) {
      throw new Error('`nodes` must be an iterable')
    }
    if (format === undefined) {
      throw new Error('`put` requires a format')
    }
    if (typeof format !== 'number') {
      throw new Error('`format` parameter must be number (multicodec)')
    }

    let options
    let formatImpl

    const generator = async function * () {
      for await (const node of nodes) {
        // Lazy load the options not when the iterator is initialized, but
github 75lb / usage-stats / lib / usage-stats-core.js View on Github external
event (category, action, options) {
    if (this._disabled) return this
    if (!(category && action)) throw new Error('category and action required')
    options = options || {}
    if (options.hitParams && t.isPlainObject(options.hitParams)) {
      options.hitParams = objToMap(options.hitParams)
    }

    let hit = this._createHit(new Map([
      [ 't', 'event' ],
      [ 'ec', category ],
      [ 'ea', action ]
    ]), options)

    if (t.isDefined(options.el)) hit.set('el', options.el)
    if (t.isDefined(options.ev)) hit.set('ev', options.ev)
    this._hits.push(hit)
    return hit
  }
github lwsjs / lws / lib / server-factory / http.js View on Github external
create (options) {
    const t = require('typical')
    const serverOptions = {}
    const server = require('http').createServer()
    if (t.isDefined(options.maxConnections)) {
      server.maxConnections = serverOptions.maxConnections = options.maxConnections
    }
    if (t.isDefined(options.keepAliveTimeout)) {
      server.keepAliveTimeout = serverOptions.keepAliveTimeout = options.keepAliveTimeout
    }
    if (Object.keys(serverOptions).length) {
      this.emit('verbose', 'server.config', serverOptions)
    }
    return server
  }
}
github 75lb / array-tools / dist / array-tools.js View on Github external
function testValue(value, test){
    if (t.isPlainObject(test)){
        return o.exists(value, test);
    } else if (Array.isArray(test)){
        var tests = test;
        return tests.some(function(test){
            return testValue(value, test);
        });    
    } else if (test instanceof RegExp){
        return test.test(value);
    } else if (typeof test === "function"){
        return test(value);
    } else {
        return test === value;
    }
}

typical

Isomorphic, functional type-checking for Javascript

MIT
Latest version published 1 month ago

Package Health Score

74 / 100
Full package analysis