UNPKG

3h-utils

Version:
366 lines (297 loc) 9.11 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : typeof define === 'function' && define.amd ? define(['exports'], factory) : (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.HUtils = {})); })(this, (function (exports) { 'use strict'; /** * Insert an element at specific index of the array. * (Throws if `index < 0 || index > array.length`.) */ var insertElement = function insertElement(array, index, element) { if (index < 0 || index > array.length) { throw new RangeError('invalid index to insert'); } if (index === 0) { array.unshift(element); } else if (index === array.length) { array.push(element); } else { array.length++; for (var i = array.length - 1; i > index; i--) { array[i] = array[i - 1]; } array[index] = element; } }; /** dts2md break */ /** * Remove specific element(s) from the array. * (Default count: `Math.max(0, array.length - start)`; * throws if `start < 0 || count < 0 || start + count > array.length`.) */ var removeElements = function removeElements(array, start, count) { if (count === void 0) { count = Math.max(0, array.length - start); } if (count < 0) { throw new RangeError('invalid count'); } if (start < 0 || start + count > array.length) { throw new RangeError('invalid index to remove'); } for (var i = start; i < array.length - count; i++) { array[i] = array[i + count]; } array.length -= count; }; /** dts2md break */ /** * Try to fit the given index into range [0, arraySize). * (Throws if `index < -arraySize || index >= arraySize`.) */ var clampIndex = function clampIndex(index, arraySize) { if (index < -arraySize || index >= arraySize) { throw new RangeError('invalid index'); } if (index < 0) { return index + arraySize; } else { return index; } }; /** dts2md break */ /** * Pick a random element from the given array. * (Throws if `array.length === 0`.) */ var pick = function pick(array) { if (array.length === 0) { throw new RangeError('no elements to pick'); } return array[Math.floor(array.length * Math.random())]; }; /** dts2md break */ /** * A stable in-place sorting function. * (Swaps two elements when `compare` returns a positive number.) */ var sort = function sort(array, compare) { var t; for (var i = 0; i < array.length - 1; i++) { for (var j = i + 1; j < array.length; j++) { if (compare(array[i], array[j]) > 0) { t = array[i]; array[i] = array[j]; array[j] = t; } } } }; /** dts2md break */ /** * Shuffle the array elements in place. */ var shuffle = function shuffle(array) { var j, t; for (var i = array.length - 1; i > 0; i--) { j = Math.floor(Math.random() * i); t = array[i]; array[i] = array[j]; array[j] = t; } }; /** dts2md break */ /** * Get an array containing the unique elements * of the given array. */ var unique = function unique(array) { var result = []; var element; for (var i = 0; i < array.length; i++) { element = array[i]; if (result.indexOf(element) === -1) { result.push(element); } } return result; }; /** dts2md break */ /** * Merge the given objects into a new one. */ var merge = function merge() { for (var _len = arguments.length, objects = new Array(_len), _key = 0; _key < _len; _key++) { objects[_key] = arguments[_key]; } return Object.assign.apply(Object, [Object.create(null)].concat(objects)); }; /** dts2md break */ /** * Returns `Object.prototype.toString.call(value) === '[object Object]'`. */ var isDict = function isDict(value) { return Object.prototype.toString.call(value) === '[object Object]'; }; /** dts2md break */ /** * Clone an object shallowly. */ var cloneShallowly = function cloneShallowly(object) { if (Array.isArray(object)) { return object.map(function (item) { return item; }); } else if (object && typeof object === 'object') { return Object.assign(Object.create(null), object); } else { return object; } }; /** dts2md break */ /** * Clone an object deeply. * (A simple, recursive implementation.) */ var cloneDeeply = function cloneDeeply(object) { if (Array.isArray(object)) { return object.map(function (item) { return cloneDeeply(item); }); } else if (object && typeof object === 'object') { var result = Object.create(null); for (var key in object) { var value = object[key]; if (value && typeof value === 'object') { result[key] = cloneDeeply(value); } else { result[key] = value; } } return result; } else { return object; } }; /** * Returns a random number between the given bounds. */ var random = function random(floor, ceiling) { return floor + (ceiling - floor) * Math.random(); }; /** dts2md break */ /** * Clamps x into range [min, max]. */ var clamp = function clamp(x, min, max) { if (x <= min) { return min; } else if (x >= max) { return max; } else { return x; } }; /** dts2md break */ /** * Returns `begin + (end - begin) * k`. */ var interpolate = function interpolate(begin, end, k) { return begin + (end - begin) * k; }; /** dts2md break */ /** * degrees -> radians */ var deg2rad = function deg2rad(deg) { return deg / 180 * Math.PI; }; /** dts2md break */ /** * radians -> degrees */ var rad2deg = function rad2deg(rad) { return rad / Math.PI * 180; }; /** dts2md break */ /** * Returns the quadratic sum of the given values. */ var quadraticSum = function quadraticSum() { for (var _len = arguments.length, values = new Array(_len), _key = 0; _key < _len; _key++) { values[_key] = arguments[_key]; } return values.reduce(function (s, x) { return s + Math.pow(x, 2); }, 0); }; /** dts2md break */ /** * Create a throttle wrapper. */ var throttle = function throttle(gap, callback) { var lastInvokeTime = null; var throttleWrapper = function throttleWrapper() { var now = Date.now(); if (lastInvokeTime === null || now - lastInvokeTime >= throttleWrapper.throttleGap) { lastInvokeTime = now; return callback.apply(void 0, arguments); } }; throttleWrapper.throttleGap = gap; return throttleWrapper; }; /** dts2md break */ /** * Create a debounce wrapper. */ var debounce = function debounce(timeout, callback) { var timer = null; var debounceWrapper = function debounceWrapper() { if (timer !== null) { clearTimeout(timer); } for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } timer = setTimeout.apply(void 0, [callback, debounceWrapper.debounceTimeout].concat(args)); }; debounceWrapper.debounceTimeout = timeout; return debounceWrapper; }; /** dts2md break */ /** * Create a lock wrapper. */ var lock = function lock(callback) { var wrapper = function wrapper() { if (!wrapper.disabled) { wrapper.disabled = true; return callback.apply(void 0, arguments); } }; wrapper.disabled = false; return wrapper; }; exports.clamp = clamp; exports.clampIndex = clampIndex; exports.cloneDeeply = cloneDeeply; exports.cloneShallowly = cloneShallowly; exports.debounce = debounce; exports.deg2rad = deg2rad; exports.insertElement = insertElement; exports.interpolate = interpolate; exports.isDict = isDict; exports.lock = lock; exports.merge = merge; exports.pick = pick; exports.quadraticSum = quadraticSum; exports.rad2deg = rad2deg; exports.random = random; exports.removeElements = removeElements; exports.shuffle = shuffle; exports.sort = sort; exports.throttle = throttle; exports.unique = unique; Object.defineProperty(exports, '__esModule', { value: true }); }));