UNPKG

data-sort

Version:

Data engine is small data management lib for some sort and filter.

346 lines (295 loc) 9.18 kB
/** @license Engine v3.1.1 * data-sort.development.js * * Copyright Jan Silhan * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ 'use strict'; (function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : typeof define === 'function' && define.amd ? define(factory) : (global.Sort = factory()); }(this, (function () { 'use strict'; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } /** * Sort engine, just basic sort provider * * @export * @class Sort */ var Sort = function () { /** * Creates an instance of Sort. * @param {any} data - original data * @param {string} [primaryKey=null] - primary key which will be fallback when keys are equals * @param {function} [sortFunction=null] - custom sort function * @param {boolean} [direction=true] - custom sort function * * @memberOf Sort */ function Sort() { var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, data = _ref.data, primaryKey = _ref.primaryKey, sortFunction = _ref.sortFunction, direction = _ref.direction; _classCallCheck(this, Sort); _initialiseProps.call(this); this.Direction = direction; this.sortFunc = this.defaultSort; this.SortFunction = sortFunction; this.PrimaryKey = primaryKey; this.Data = data; } /** * Update data, refresh old data with new. * * @param {any} data new data * @param {boolean} shouldSort should be resorted * @memberOf Sort */ _createClass(Sort, [{ key: 'Data', set: function (data) { this.setData(data); } /** * Setter for custom function * * @param {function} func your custom sort function * @memberOf Sort */ , get: function () { return this.data; } }, { key: 'SortFunction', set: function (func) { this.setSortFunction(func); } /** * Setter for direction * @param {boolean} direction of sort */ }, { key: 'Direction', set: function (direction) { if (typeof direction === 'boolean') this.direction = direction;else this.direction = true; } /** * Getter for direction * @returns {boolean} */ , get: function () { return this.direction; } /** * Setter for primary key (fallback key) * * @param {string} key primary key * @memberOf Sort */ }, { key: 'PrimaryKey', set: function (key) { this.setPrimaryKey(key); } /** * Getter for primary key * @returns {string|null} */ , get: function () { return this.primaryKey; } /** * Remover primary key set to default * * * @memberOf Sort */ /** * Setup default sort function * * * @memberOf Sort */ /** * Compare primary key * Fallback function when current values are equal. * @param {any} a - first item * @param {any} b - second item * @return {number} position of elements * @memberOf Sort */ /** * Compare by current name * * @param {any} a - first item * @param {any} b - second item * @return {number} position of elements * @memberOf Sort */ /** * sort by name, sets new name and check if we need to only reverse * * @param {string} name key for sort * @memberOf Sort */ /** * Setting right function for sort, * @private */ /** * default sort with key * Default sorting function when user won't add own function. * @private * @param {any} a - first item * @param {any} b - second item * @return {number} position of elements * @memberOf Sort */ /** * default sort * Default sorting function when user won't add own function. * * @param {any} a - first item * @param {any} b - second item * @return {number} position of elements * @memberOf Sort */ /** * Well just sort function. when we need resort. * * @return {Array} sorted data * @memberOf Sort */ /** * well just reverse sorted array * * @return {Array} reversed data * @memberOf Sort */ /** * Getter for data * * * @memberOf Sort */ }]); return Sort; }(); var _initialiseProps = function () { var _this = this; this.primaryKey = null; this.direction = true; this.data = []; this.currentName = null; this.setData = function (data) { var shouldSort = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true; if (!Array.isArray(data)) { _this.data = []; return []; } _this.data = data; if (shouldSort) { _this.sortData(); } return _this.data; }; this.setSortFunction = function (func) { if (typeof func === 'function') { _this.sortFunc = func; _this.isCustomFunction = true; } }; this.setPrimaryKey = function (key) { if (_this.isCustomFunction) { return; } if (typeof key === 'string' && key.length > 0) { _this.primaryKey = key; if (_this.currentName === null) { _this.currentName = key; } _this.setOwnSortFunction(); } }; this.removePrimaryKey = function () { _this.primaryKey = null; _this.setOwnSortFunction(); }; this.setDefaultSort = function () { _this.isCustomFunction = false; _this.setOwnSortFunction(); }; this.comparePrimaryKey = function (a, b) { var first = a[_this.primaryKey]; var second = b[_this.primaryKey]; if (first === second) return 0; return first > second === _this.direction ? 1 : -1; }; this.compare = function (a, b) { var first = a[_this.currentName]; var second = b[_this.currentName]; if (first === second) return 0; return first > second === _this.direction ? 1 : -1; }; this.sortBy = function (name) { if (_this.currentName === name) { return _this.reverseData(); } _this.currentName = name; _this.setOwnSortFunction(); return _this.sortData(); }; this.setOwnSortFunction = function () { var isCustomFunction = _this.isCustomFunction, primaryKey = _this.primaryKey, currentName = _this.currentName; if (isCustomFunction) { return; } if (primaryKey !== null && primaryKey !== currentName) { _this.sortFunc = _this.defaultSortWithKey; } else { _this.sortFunc = _this.defaultSort; } }; this.defaultSortWithKey = function (a, b) { var first = a[_this.currentName]; var second = b[_this.currentName]; if (first === second) { return _this.comparePrimaryKey(a, b); } return first > second === _this.direction ? 1 : -1; }; this.defaultSort = function (a, b) { return _this.compare(a, b); }; this.sortData = function () { if (_this.currentName) { _this.data = _this.data.sort(_this.sortFunc); } return _this.data; }; this.reverseData = function () { _this.data = _this.data.reverse(); return _this.data; }; this.getData = function () { return _this.Data; }; }; var dataSort = Object.freeze({ default: Sort }); var Sort$2 = ( dataSort && Sort ) || dataSort; var dataSort$1 = Sort$2['default'] ? Sort$2['default'] : Sort$2; return dataSort$1; })));