UNPKG

buckets-js

Version:

Buckets is a complete, fully tested and documented data structure library written in pure JavaScript.

112 lines (101 loc) 2.06 kB
'use strict'; /** * Top level namespace for Buckets, * a JavaScript data structure library. * @name buckets */ var buckets = {}; /** * Default function to compare element order. * @function * @private */ buckets.defaultCompare = function (a, b) { if (a < b) { return -1; } if (a === b) { return 0; } return 1; }; /** * Default function to test equality. * @function * @private */ buckets.defaultEquals = function (a, b) { return a === b; }; /** * Default function to convert an object to a string. * @function * @private */ buckets.defaultToString = function (item) { if (item === null) { return 'BUCKETS_NULL'; } if (buckets.isUndefined(item)) { return 'BUCKETS_UNDEFINED'; } if (buckets.isString(item)) { return item; } return item.toString(); }; /** * Checks if the given argument is a function. * @function * @private */ buckets.isFunction = function (func) { return (typeof func) === 'function'; }; /** * Checks if the given argument is undefined. * @function * @private */ buckets.isUndefined = function (obj) { return obj === undefined; }; /** * Checks if the given argument is a string. * @function * @private */ buckets.isString = function (obj) { return Object.prototype.toString.call(obj) === '[object String]'; }; /** * Reverses a compare function. * @function * @private */ buckets.reverseCompareFunction = function (compareFunction) { if (!buckets.isFunction(compareFunction)) { return function (a, b) { if (a < b) { return 1; } if (a === b) { return 0; } return -1; }; } return function (d, v) { return compareFunction(d, v) * -1; }; }; /** * Returns an equal function given a compare function. * @function * @private */ buckets.compareToEquals = function (compareFunction) { return function (a, b) { return compareFunction(a, b) === 0; }; };