UNPKG

mathball

Version:

A JavaScript library for Competitive Programming

29 lines (25 loc) 1.09 kB
'use strict'; var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; /* Function: length() */ function validate(arg) { var types = ['number', 'object', 'string']; if (types.indexOf(typeof arg === 'undefined' ? 'undefined' : _typeof(arg)) == -1 || String(arg) == 'null' || typeof arg == 'number' && (arg + 1 == arg || arg != arg)) { throw new TypeError('Invalid argument received: ' + JSON.stringify(arg) + '\n\'length()\' only accept either a real number, string, object or array!\n'); } } module.exports = function (item) { validate(item); switch (item.constructor) { case Number: if (Number.isInteger(item)) return item.toString().length;else { var arr = item.toString().split(''); arr.splice(arr.indexOf('.'), 1); return arr.length; } case String: case Array: return item.length; case Object: return Object.keys(item).length; } };