• Jump To … +
    Either.js List.js Maybe.js Set.js Stream.js Thunk.js Tuple.js boolean.js char.js base.js extra.js fold.js map.js zipWith.js collection.js control.js curried.js eq.js floating.js fractional.js function.js integral.js match.js num.js numeric.js object.js ord.js realfrac.js register.js string.js type.js nodash.js
  • type.js

  • ¶
    /* vim: set et sw=2 ts=2: */
    'use strict';
    
    module.exports = function () {
    
      var Nodash = this;
    
      var toString = Object.prototype.toString;
    
      function classOf(thing) {
        var exactType = toString.call(thing);
        return exactType.slice(8, exactType.length - 1).toLowerCase();
      }
    
      return {
    
        classOf: classOf,
    
        typeOf: function (thing) {
          var type = typeof thing;
          switch (type) {
            case 'number':
              return isNaN(thing) ? 'not-a-number' : 'number';
            case 'object':
              var exactType = classOf(thing);
              if (exactType === 'object' && thing.constructor && thing.constructor.__type) {
                return thing.constructor.__type;
              }
              return exactType;
            default:
              return type;
          }
        },
    
        is: function (type, thing) {
          return thing instanceof type;
        },
    
        isBoolean: function (thing) {
          return typeof thing === 'boolean';
        },
    
        isNumber: function (thing) {
          return typeof thing === 'number';
        },
    
        isString: function (thing) {
          return typeof thing === 'string' || toString.call(thing) === '[object String]';
        },
    
        isFunction: function (thing) {
          return typeof thing === 'function';
        },
    
        isUndefined: function (thing) {
          return typeof thing === 'undefined';
        },
    
        isRegExp: function (thing) {
          return toString.call(thing) === '[object RegExp]';
        },
    
        isDate: function (thing) {
          return toString.call(thing) === '[object Date]';
        },
    
        isNull: function (thing) {
          return toString.call(thing) === '[object Null]';
        },
    
        isArguments: function (thing) {
          return toString.call(thing) === '[object Arguments]';
        },
    
        isObject: function (thing) {
          return typeof thing === 'object' &&
              thing !== null && !Array.isArray(thing);
        },
    
        'isNumeric isDigit': function (thing) {
          return /^[0-9]+$/.test(thing);
        },
    
        isInteger: function (thing) {
          return Nodash.isNumber(thing) && !isNaN(thing) &&
            thing - Math.floor(thing) === 0 &&
            thing !== Infinity && thing !== -Infinity;
        },
    
        isArray: Array.isArray
      };
    };