How to use type-detect - 10 common examples

To help you get started, we’ve selected a few type-detect 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 sezna / nps / src / bin-utils / index.js View on Github external
if (isUndefined(config)) {
    // let the caller deal with this
    return config
  }
  let typeMessage = `Your config data type was`
  if (isFunction(config)) {
    config = config(input)
    typeMessage = `${typeMessage} a function which returned`
  }
  const emptyConfig = isEmpty(config)
  const plainObjectConfig = isPlainObject(config)
  if (plainObjectConfig && emptyConfig) {
    typeMessage = `${typeMessage} an object, but it was empty`
  } else {
    typeMessage = `${typeMessage} a data type of "${typeOf(config)}"`
  }
  if (!plainObjectConfig || emptyConfig) {
    log.error({
      message: chalk.red(
        oneLine`
          The package-scripts configuration
          ("${configPath.replace(/\\/g, '/')}") must be a non-empty object
          or a function that returns a non-empty object.
        `,
      ),
      ref: 'config-must-be-an-object',
    })
    throw new Error(typeMessage)
  }

  const defaultConfig = {
github michaelBenin / react-ssr-spa / test / server / utils / test_teardown_util.js View on Github external
it('Should close connections without error.', function(done) {
    this.timeout(10000);
    expect(typeDetect(createOrGetServer)).to.equal('function');

    if (redisClient.quit) {
      redisClient.quit();
    }

    createOrGetServer().close();
    done();
  });
});
github elastic / kibana / packages / kbn-config-schema / src / types / object_type.ts View on Github external
protected handleError(type: string, { reason, value }: Record) {
    switch (type) {
      case 'any.required':
      case 'object.base':
        return `expected a plain object value, but found [${typeDetect(value)}] instead.`;
      case 'object.allowUnknown':
        return `definition for this key is missing`;
      case 'object.child':
        return reason[0];
    }
  }
github elastic / kibana / x-pack / plugins / security / server / authentication / providers / oidc.ts View on Github external
constructor(
    protected readonly options: Readonly,
    oidcOptions?: Readonly
  ) {
    super(options);
    if (!oidcOptions || !oidcOptions.realm) {
      throw new Error('Realm name must be specified');
    }

    if (type(oidcOptions.realm) !== 'string') {
      throw new Error('Realm must be a string');
    }

    this.realm = oidcOptions.realm as string;
  }
github elastic / kibana / src / core / server / http / router / response_adapter.ts View on Github external
public handle(kibanaResponse: KibanaResponse) {
    if (!(kibanaResponse instanceof KibanaResponse)) {
      throw new Error(
        `Unexpected result from Route Handler. Expected KibanaResponse, but given: ${typeDetect(
          kibanaResponse
        )}.`
      );
    }

    return this.toHapiResponse(kibanaResponse);
  }
github elastic / kibana / packages / kbn-config-schema / src / types / boolean_type.ts View on Github external
protected handleError(type: string, { value }: Record) {
    if (type === 'any.required' || type === 'boolean.base') {
      return `expected value of type [boolean] but got [${typeDetect(value)}]`;
    }
  }
}
github elastic / kibana / src / legacy / server / capabilities / merge_capabilities.ts View on Github external
(a: any, b: any) => {
      if (
        (typeDetect(a) === 'boolean' && typeDetect(b) === 'Object') ||
        (typeDetect(b) === 'boolean' && typeDetect(a) === 'Object')
      ) {
        throw new Error(`a boolean and an object can't be merged`);
      }

      if (typeDetect(a) === 'boolean' && typeDetect(b) === 'boolean' && a !== b) {
        throw new Error(`"true" and "false" can't be merged`);
      }
    }
  );
github elastic / kibana / src / core / server / plugins / plugin.ts View on Github external
throw new Error(`Plugin "${this.name}" does not export "plugin" definition (${this.path}).`);
    }

    const { plugin: initializer } = pluginDefinition as {
      plugin: PluginInitializer;
    };
    if (!initializer || typeof initializer !== 'function') {
      throw new Error(`Definition of plugin "${this.name}" should be a function (${this.path}).`);
    }

    const instance = initializer(this.initializerContext);
    if (!instance || typeof instance !== 'object') {
      throw new Error(
        `Initializer for plugin "${
          this.manifest.id
        }" is expected to return plugin instance, but returned "${typeDetect(instance)}".`
      );
    }

    if (typeof instance.setup !== 'function') {
      throw new Error(`Instance of plugin "${this.name}" does not define "setup" function.`);
    }

    return instance;
  }
}
github elastic / kibana / x-pack / legacy / plugins / encrypted_saved_objects / server / lib / encrypted_saved_objects_service.ts View on Github external
if (typeRegistration === undefined) {
      return attributes;
    }

    const encryptionAAD = this.getAAD(typeRegistration, descriptor, attributes);
    const decryptedAttributes: Record = {};
    for (const attributeName of typeRegistration.attributesToEncrypt) {
      const attributeValue = attributes[attributeName];
      if (attributeValue == null) {
        continue;
      }

      if (typeof attributeValue !== 'string') {
        this.audit.decryptAttributeFailure(attributeName, descriptor);
        throw new Error(
          `Encrypted "${attributeName}" attribute should be a string, but found ${typeDetect(
            attributeValue
          )}`
        );
      }

      try {
        decryptedAttributes[attributeName] = await this.crypto.decrypt(
          attributeValue,
          encryptionAAD
        );
      } catch (err) {
        this.log.error(`Failed to decrypt "${attributeName}" attribute: ${err.message || err}`);
        this.audit.decryptAttributeFailure(attributeName, descriptor);

        throw new EncryptionError(
          `Unable to decrypt attribute "${attributeName}"`,
github qualiancy / seed / lib / filter / util.js View on Github external
/*!
 * Seed :: Query (utils) - MongoDB style array filtering
 * Copyright(c) 2011-2014 Jake Luer 
 * MIT Licensed
 */

var pathval = require('pathval');
var type = require('type-detect').Library();

var comparators = require('./comparators');

/*!
 * Custom type
 */

type.define('simplex', function(o) {
  if ('string' === type.of(o)) return true;
  if ('number' === type.of(o)) return true;
  if ('boolean' === type.of(o)) return true;
  return false;
})

/*!
 * Given the query input, create a re-usable definition

type-detect

Improved typeof detection for node.js and the browser.

MIT
Latest version published 5 months ago

Package Health Score

76 / 100
Full package analysis

Popular type-detect functions