Sha256: 57c71dec286b4dadf8d87fa450a3f3600dbd504c4ed53ffa60775515f38f000c

Contents?: true

Size: 1.48 KB

Versions: 23

Compression:

Stored size: 1.48 KB

Contents

'use strict'

module.exports = convert

function convert(test) {
  if (typeof test === 'string') {
    return typeFactory(test)
  }

  if (test === null || test === undefined) {
    return ok
  }

  if (typeof test === 'object') {
    return ('length' in test ? anyFactory : matchesFactory)(test)
  }

  if (typeof test === 'function') {
    return test
  }

  throw new Error('Expected function, string, or object as test')
}

function convertAll(tests) {
  var results = []
  var length = tests.length
  var index = -1

  while (++index < length) {
    results[index] = convert(tests[index])
  }

  return results
}

// Utility assert each property in `test` is represented in `node`, and each
// values are strictly equal.
function matchesFactory(test) {
  return matches

  function matches(node) {
    var key

    for (key in test) {
      if (node[key] !== test[key]) {
        return false
      }
    }

    return true
  }
}

function anyFactory(tests) {
  var checks = convertAll(tests)
  var length = checks.length

  return matches

  function matches() {
    var index = -1

    while (++index < length) {
      if (checks[index].apply(this, arguments)) {
        return true
      }
    }

    return false
  }
}

// Utility to convert a string into a function which checks a given node’s type
// for said string.
function typeFactory(test) {
  return type

  function type(node) {
    return Boolean(node && node.type === test)
  }
}

// Utility to return true.
function ok() {
  return true
}

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
trusty-cms-5.0.7 node_modules/unist-util-is/convert.js
trusty-cms-5.0.6 node_modules/unist-util-is/convert.js
trusty-cms-5.0.5 node_modules/unist-util-is/convert.js
trusty-cms-5.0.4 node_modules/unist-util-is/convert.js
trusty-cms-5.0.3 node_modules/unist-util-is/convert.js
trusty-cms-5.0.2 node_modules/unist-util-is/convert.js
trusty-cms-5.0.1 node_modules/unist-util-is/convert.js
trusty-cms-4.3.5 node_modules/unist-util-is/convert.js
trusty-cms-5.0.0 node_modules/unist-util-is/convert.js
trusty-cms-4.3.4 node_modules/unist-util-is/convert.js
trusty-cms-4.3.3 node_modules/unist-util-is/convert.js
trusty-cms-4.3.2 node_modules/unist-util-is/convert.js
trusty-cms-4.3.1 node_modules/unist-util-is/convert.js
trusty-cms-4.3 node_modules/unist-util-is/convert.js
trusty-cms-4.2.3 node_modules/unist-util-is/convert.js
trusty-cms-4.2.2 node_modules/unist-util-is/convert.js
trusty-cms-4.2.1 node_modules/unist-util-is/convert.js
trusty-cms-4.2 node_modules/unist-util-is/convert.js
trusty-cms-4.1.9 node_modules/unist-util-is/convert.js
trusty-cms-4.1.8 node_modules/unist-util-is/convert.js