How to use type-check - 10 common examples

To help you get started, we’ve selected a few type-check 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 delvedor / appversion / lib / read.js View on Github external
function readJson (file) {
  if (!check('String', file)) return
  if (file.slice(-5) !== '.json') {
    console.log(chalk.red(`\n${chalk.bold('AppVersion:')} ${file} is not a Json\n`))
    process.exit(1)
  }
  try {
    let obj = require(file === JSON_FILE ? resolve('./', file) : resolve(file))
    // checks if the appversion.json is at the latest version
    if (file === JSON_FILE && (!obj.config || obj.config.appversion !== apvVersion)) obj = updateAppversion(obj)
    return obj
  } catch (err) {
    if (err.code === 'MODULE_NOT_FOUND') {
      console.log(chalk.red(`\n${chalk.bold('AppVersion:')} Could not find appversion.json\nType ${chalk.bold('\'apv init\'')} for generate the file and start use AppVersion.\n`))
      process.exit(1)
    } else {
      throw new Error(err)
    }
github delvedor / appversion / lib / set.js View on Github external
function setStatus (param) {
  if (!check('String', param)) return
  let obj = readJson(JSON_FILE)
  let previousObj = {
    status: {
      stage: obj.status.stage,
      number: obj.status.number
    }
  }
  let status = param.split('.')
  if (status[1] && !check('Number', Number(status[1]))) {
    console.log(chalk.red(`\n${chalk.bold('AppVersion:')} Insert a valid status.number number\n`))
    return
  }
  let match = ['Stable', 'stable', 'RC', 'rc', 'Beta', 'beta', 'Alpha', 'alpha']
  if (match.indexOf(status[0]) === -1) {
    console.log(chalk.red(`\n${chalk.bold('AppVersion:')} Insert a valid status.stage string\n`))
    return
  }
  obj.status.stage = status[0]
  // if there's not the status number, it's setted to zero
  obj.status.number = Number(status[1]) || 0
  writeJson(obj, chalk.green(`\n${chalk.bold('AppVersion:')} Status updated to ${status[0]}.${status[1] || 0}\n`))
  createBadge('status', true, previousObj)
}
github sundowndev / underbase / migrations.pkg / migrations.js View on Github external
function createLogger(prefix) {
  check(prefix, String);

  // Return noop if logging is disabled.
  if(Migrations.options.log === false) {
    return function() {};
  }

  return function(level, ...args) {
    //check(level, Match.OneOf('info', 'error', 'warn', 'debug'));
    var logger = Migrations.options && Migrations.options.logger;

    if(logger && _.isFunction(logger)) {

      logger({level, ...args });

    } else {
      console[level]({ level, ...args });
github apifytech / actor-scraper / src / modules / utils.js View on Github external
export const checkParamOrThrow = (value, name, type, message) => {
    if (!message) message = `Parameter "${name}" of type ${type} must be provided`;

    const allowedTypes = parseType(type);

    const allowsBuffer = allowedTypes.filter(item => item.type === 'Buffer').length;
    const allowsPromise = allowedTypes.filter(item => item.type === 'Promise').length;

    if (allowsBuffer && Buffer.isBuffer(value)) return;
    if (allowsPromise && typeof value.then === 'function') return;

    // This will ignore Buffer type.
    if (!parsedTypeCheck(allowedTypes, value)) throw new Error(message);
};
github apifytech / actor-scraper / src / modules / utils.js View on Github external
export const checkParamOrThrow = (value, name, type, message) => {
    if (!message) message = `Parameter "${name}" of type ${type} must be provided`;

    const allowedTypes = parseType(type);

    const allowsBuffer = allowedTypes.filter(item => item.type === 'Buffer').length;
    const allowsPromise = allowedTypes.filter(item => item.type === 'Promise').length;

    if (allowsBuffer && Buffer.isBuffer(value)) return;
    if (allowsPromise && typeof value.then === 'function') return;

    // This will ignore Buffer type.
    if (!parsedTypeCheck(allowedTypes, value)) throw new Error(message);
};
github delvedor / appversion / appversion.js View on Github external
function composePattern (pattern, callback) {
  if (!check('String', pattern)) throw new Error('composePattern() -> pattern is not a string')
  if (!check('Function', callback)) throw new Error('composePattern() -> callback is not a function')
  pattern = pattern.split('')
  getAppVersion((err, obj) => {
    if (err) console.log(err)
    let ptt = ''
    pattern.map((ele) => {
      ptt += switchPattern(obj, ele)
    })
    callback(ptt)
  })
}
github apifytech / actor-page-analyzer / src / index.js View on Github external
input.pages.forEach(page => {
                if (!typeCheck(PAGE_INPUT_TYPE, page) && !isSinglePageInput) {
                    log('Expected input:');
                    log(INPUT_TYPE);
                    log('Received input:');
                    console.dir(input);
                    throw new Error('Received invalid input');
                }
            });
        } else if (isSinglePageInput) {
github delvedor / appversion / appversion.js View on Github external
function switchPattern (obj, pattern) {
  if (!check('String', pattern)) throw new Error('switchPattern() -> pattern is not a string')
  if (!check('Object', obj)) throw new Error('switchPattern() -> obj is not an object')
  switch (pattern) {
    case 'M' || 'B':
      return obj.version.major
    case 'm' || 'F':
      return obj.version.minor
    case 'p' || 'f':
      return obj.version.patch
    case 'S':
      return obj.status.stage
    case 's':
      return obj.status.number
    case 'n':
      return obj.build.number
    case 't':
      return obj.build.total
github zmts / supra-api-nodejs / util / typechecker.js View on Github external
module.exports.middleware = (req, res, next) => {
  if (!typeCheck('Object', req)) {
    throw new ErrorWrapper('req argument wrong type', 500)
  }
  if (!typeCheck('Object', res)) {
    throw new ErrorWrapper('res argument wrong type', 500)
  }
  if (next && !typeCheck('Function', next)) {
    throw new ErrorWrapper('next  argument wrong type', 500)
  }
}
github zmts / supra-api-nodejs / util / typechecker.js View on Github external
module.exports.main = (argument, type, required) => {
  /**
   * check if required
   */
  if (required && !argument) {
    throw new ErrorWrapper('Required parameters not supplied', 500)
  }
  /**
   * check arguments type
   */
  const isArgumentExist = [null, false, NaN].includes(argument) || argument
  if (isArgumentExist && !typeCheck(type, argument)) {
    throw new ErrorWrapper('Argument wrong type', 500)
  }
}

type-check

type-check allows you to check the types of JavaScript values at runtime with a Haskell like type syntax.

MIT
Latest version published 5 years ago

Package Health Score

67 / 100
Full package analysis