| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- (function (global, factory) {
- typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/core')) :
- typeof define === 'function' && define.amd ? define('ngx-order-pipe', ['exports', '@angular/core'], factory) :
- (global = global || self, factory(global['ngx-order-pipe'] = {}, global.ng.core));
- }(this, function (exports, core) { 'use strict';
- /*! *****************************************************************************
- Copyright (c) Microsoft Corporation. All rights reserved.
- Licensed under the Apache License, Version 2.0 (the "License"); you may not use
- this file except in compliance with the License. You may obtain a copy of the
- License at http://www.apache.org/licenses/LICENSE-2.0
- THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
- WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
- MERCHANTABLITY OR NON-INFRINGEMENT.
- See the Apache Version 2.0 License for specific language governing permissions
- and limitations under the License.
- ***************************************************************************** */
- function __decorate(decorators, target, key, desc) {
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
- return c > 3 && r && Object.defineProperty(target, key, r), r;
- }
- var OrderPipe = /** @class */ (function () {
- function OrderPipe() {
- }
- OrderPipe_1 = OrderPipe;
- /**
- * Check if a value is a string
- *
- * @param value
- */
- OrderPipe.isString = function (value) {
- return typeof value === "string" || value instanceof String;
- };
- /**
- * Sorts values ignoring the case
- *
- * @param a
- * @param b
- */
- OrderPipe.caseInsensitiveSort = function (a, b) {
- if (OrderPipe_1.isString(a) && OrderPipe_1.isString(b)) {
- return a.localeCompare(b);
- }
- return OrderPipe_1.defaultCompare(a, b);
- };
- /**
- * Default compare method
- *
- * @param a
- * @param b
- */
- OrderPipe.defaultCompare = function (a, b) {
- if (a === b) {
- return 0;
- }
- if (a == null) {
- return 1;
- }
- if (b == null) {
- return -1;
- }
- return a > b ? 1 : -1;
- };
- /**
- * Parse expression, split into items
- * @param expression
- * @returns {string[]}
- */
- OrderPipe.parseExpression = function (expression) {
- expression = expression.replace(/\[(\w+)\]/g, ".$1");
- expression = expression.replace(/^\./, "");
- return expression.split(".");
- };
- /**
- * Get value by expression
- *
- * @param object
- * @param expression
- * @returns {any}
- */
- OrderPipe.getValue = function (object, expression) {
- for (var i = 0, n = expression.length; i < n; ++i) {
- if (!object) {
- return;
- }
- var k = expression[i];
- if (!(k in object)) {
- return;
- }
- if (typeof object[k] === "function") {
- object = object[k]();
- }
- else {
- object = object[k];
- }
- }
- return object;
- };
- /**
- * Set value by expression
- *
- * @param object
- * @param value
- * @param expression
- */
- OrderPipe.setValue = function (object, value, expression) {
- var i;
- for (i = 0; i < expression.length - 1; i++) {
- object = object[expression[i]];
- }
- object[expression[i]] = value;
- };
- OrderPipe.prototype.transform = function (value, expression, reverse, isCaseInsensitive, comparator) {
- if (isCaseInsensitive === void 0) { isCaseInsensitive = false; }
- if (!value) {
- return value;
- }
- if (Array.isArray(expression)) {
- return this.multiExpressionTransform(value, expression, reverse, isCaseInsensitive, comparator);
- }
- if (Array.isArray(value)) {
- return this.sortArray(value.slice(), expression, reverse, isCaseInsensitive, comparator);
- }
- if (typeof value === "object") {
- return this.transformObject(Object.assign({}, value), expression, reverse, isCaseInsensitive, comparator);
- }
- return value;
- };
- /**
- * Sort array
- *
- * @param value
- * @param expression
- * @param reverse
- * @param isCaseInsensitive
- * @param comparator
- * @returns {any[]}
- */
- OrderPipe.prototype.sortArray = function (value, expression, reverse, isCaseInsensitive, comparator) {
- var isDeepLink = expression && expression.indexOf(".") !== -1;
- if (isDeepLink) {
- expression = OrderPipe_1.parseExpression(expression);
- }
- var compareFn;
- if (comparator && typeof comparator === "function") {
- compareFn = comparator;
- }
- else {
- compareFn = isCaseInsensitive
- ? OrderPipe_1.caseInsensitiveSort
- : OrderPipe_1.defaultCompare;
- }
- var array = value.sort(function (a, b) {
- if (!expression) {
- return compareFn(a, b);
- }
- if (!isDeepLink) {
- if (a && b) {
- return compareFn(a[expression], b[expression]);
- }
- return compareFn(a, b);
- }
- return compareFn(OrderPipe_1.getValue(a, expression), OrderPipe_1.getValue(b, expression));
- });
- if (reverse) {
- return array.reverse();
- }
- return array;
- };
- /**
- * Transform Object
- *
- * @param value
- * @param expression
- * @param reverse
- * @param isCaseInsensitive
- * @param comparator
- * @returns {any[]}
- */
- OrderPipe.prototype.transformObject = function (value, expression, reverse, isCaseInsensitive, comparator) {
- var parsedExpression = OrderPipe_1.parseExpression(expression);
- var lastPredicate = parsedExpression.pop();
- var oldValue = OrderPipe_1.getValue(value, parsedExpression);
- if (!Array.isArray(oldValue)) {
- parsedExpression.push(lastPredicate);
- lastPredicate = null;
- oldValue = OrderPipe_1.getValue(value, parsedExpression);
- }
- if (!oldValue) {
- return value;
- }
- OrderPipe_1.setValue(value, this.transform(oldValue, lastPredicate, reverse, isCaseInsensitive), parsedExpression);
- return value;
- };
- /**
- * Apply multiple expressions
- *
- * @param value
- * @param {any[]} expressions
- * @param {boolean} reverse
- * @param {boolean} isCaseInsensitive
- * @param {Function} comparator
- * @returns {any}
- */
- OrderPipe.prototype.multiExpressionTransform = function (value, expressions, reverse, isCaseInsensitive, comparator) {
- var _this = this;
- if (isCaseInsensitive === void 0) { isCaseInsensitive = false; }
- return expressions.reverse().reduce(function (result, expression) {
- return _this.transform(result, expression, reverse, isCaseInsensitive, comparator);
- }, value);
- };
- var OrderPipe_1;
- OrderPipe = OrderPipe_1 = __decorate([
- core.Pipe({
- name: "orderBy",
- pure: false
- })
- ], OrderPipe);
- return OrderPipe;
- }());
- var OrderModule = /** @class */ (function () {
- function OrderModule() {
- }
- OrderModule = __decorate([
- core.NgModule({
- declarations: [OrderPipe],
- exports: [OrderPipe],
- providers: [OrderPipe]
- })
- ], OrderModule);
- return OrderModule;
- }());
- exports.OrderModule = OrderModule;
- exports.OrderPipe = OrderPipe;
- Object.defineProperty(exports, '__esModule', { value: true });
- }));
- //# sourceMappingURL=ngx-order-pipe.umd.js.map
|