ngx-order-pipe.umd.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. (function (global, factory) {
  2. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/core')) :
  3. typeof define === 'function' && define.amd ? define('ngx-order-pipe', ['exports', '@angular/core'], factory) :
  4. (global = global || self, factory(global['ngx-order-pipe'] = {}, global.ng.core));
  5. }(this, function (exports, core) { 'use strict';
  6. /*! *****************************************************************************
  7. Copyright (c) Microsoft Corporation. All rights reserved.
  8. Licensed under the Apache License, Version 2.0 (the "License"); you may not use
  9. this file except in compliance with the License. You may obtain a copy of the
  10. License at http://www.apache.org/licenses/LICENSE-2.0
  11. THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  12. KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
  13. WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
  14. MERCHANTABLITY OR NON-INFRINGEMENT.
  15. See the Apache Version 2.0 License for specific language governing permissions
  16. and limitations under the License.
  17. ***************************************************************************** */
  18. function __decorate(decorators, target, key, desc) {
  19. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  20. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  21. 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;
  22. return c > 3 && r && Object.defineProperty(target, key, r), r;
  23. }
  24. var OrderPipe = /** @class */ (function () {
  25. function OrderPipe() {
  26. }
  27. OrderPipe_1 = OrderPipe;
  28. /**
  29. * Check if a value is a string
  30. *
  31. * @param value
  32. */
  33. OrderPipe.isString = function (value) {
  34. return typeof value === "string" || value instanceof String;
  35. };
  36. /**
  37. * Sorts values ignoring the case
  38. *
  39. * @param a
  40. * @param b
  41. */
  42. OrderPipe.caseInsensitiveSort = function (a, b) {
  43. if (OrderPipe_1.isString(a) && OrderPipe_1.isString(b)) {
  44. return a.localeCompare(b);
  45. }
  46. return OrderPipe_1.defaultCompare(a, b);
  47. };
  48. /**
  49. * Default compare method
  50. *
  51. * @param a
  52. * @param b
  53. */
  54. OrderPipe.defaultCompare = function (a, b) {
  55. if (a === b) {
  56. return 0;
  57. }
  58. if (a == null) {
  59. return 1;
  60. }
  61. if (b == null) {
  62. return -1;
  63. }
  64. return a > b ? 1 : -1;
  65. };
  66. /**
  67. * Parse expression, split into items
  68. * @param expression
  69. * @returns {string[]}
  70. */
  71. OrderPipe.parseExpression = function (expression) {
  72. expression = expression.replace(/\[(\w+)\]/g, ".$1");
  73. expression = expression.replace(/^\./, "");
  74. return expression.split(".");
  75. };
  76. /**
  77. * Get value by expression
  78. *
  79. * @param object
  80. * @param expression
  81. * @returns {any}
  82. */
  83. OrderPipe.getValue = function (object, expression) {
  84. for (var i = 0, n = expression.length; i < n; ++i) {
  85. if (!object) {
  86. return;
  87. }
  88. var k = expression[i];
  89. if (!(k in object)) {
  90. return;
  91. }
  92. if (typeof object[k] === "function") {
  93. object = object[k]();
  94. }
  95. else {
  96. object = object[k];
  97. }
  98. }
  99. return object;
  100. };
  101. /**
  102. * Set value by expression
  103. *
  104. * @param object
  105. * @param value
  106. * @param expression
  107. */
  108. OrderPipe.setValue = function (object, value, expression) {
  109. var i;
  110. for (i = 0; i < expression.length - 1; i++) {
  111. object = object[expression[i]];
  112. }
  113. object[expression[i]] = value;
  114. };
  115. OrderPipe.prototype.transform = function (value, expression, reverse, isCaseInsensitive, comparator) {
  116. if (isCaseInsensitive === void 0) { isCaseInsensitive = false; }
  117. if (!value) {
  118. return value;
  119. }
  120. if (Array.isArray(expression)) {
  121. return this.multiExpressionTransform(value, expression, reverse, isCaseInsensitive, comparator);
  122. }
  123. if (Array.isArray(value)) {
  124. return this.sortArray(value.slice(), expression, reverse, isCaseInsensitive, comparator);
  125. }
  126. if (typeof value === "object") {
  127. return this.transformObject(Object.assign({}, value), expression, reverse, isCaseInsensitive, comparator);
  128. }
  129. return value;
  130. };
  131. /**
  132. * Sort array
  133. *
  134. * @param value
  135. * @param expression
  136. * @param reverse
  137. * @param isCaseInsensitive
  138. * @param comparator
  139. * @returns {any[]}
  140. */
  141. OrderPipe.prototype.sortArray = function (value, expression, reverse, isCaseInsensitive, comparator) {
  142. var isDeepLink = expression && expression.indexOf(".") !== -1;
  143. if (isDeepLink) {
  144. expression = OrderPipe_1.parseExpression(expression);
  145. }
  146. var compareFn;
  147. if (comparator && typeof comparator === "function") {
  148. compareFn = comparator;
  149. }
  150. else {
  151. compareFn = isCaseInsensitive
  152. ? OrderPipe_1.caseInsensitiveSort
  153. : OrderPipe_1.defaultCompare;
  154. }
  155. var array = value.sort(function (a, b) {
  156. if (!expression) {
  157. return compareFn(a, b);
  158. }
  159. if (!isDeepLink) {
  160. if (a && b) {
  161. return compareFn(a[expression], b[expression]);
  162. }
  163. return compareFn(a, b);
  164. }
  165. return compareFn(OrderPipe_1.getValue(a, expression), OrderPipe_1.getValue(b, expression));
  166. });
  167. if (reverse) {
  168. return array.reverse();
  169. }
  170. return array;
  171. };
  172. /**
  173. * Transform Object
  174. *
  175. * @param value
  176. * @param expression
  177. * @param reverse
  178. * @param isCaseInsensitive
  179. * @param comparator
  180. * @returns {any[]}
  181. */
  182. OrderPipe.prototype.transformObject = function (value, expression, reverse, isCaseInsensitive, comparator) {
  183. var parsedExpression = OrderPipe_1.parseExpression(expression);
  184. var lastPredicate = parsedExpression.pop();
  185. var oldValue = OrderPipe_1.getValue(value, parsedExpression);
  186. if (!Array.isArray(oldValue)) {
  187. parsedExpression.push(lastPredicate);
  188. lastPredicate = null;
  189. oldValue = OrderPipe_1.getValue(value, parsedExpression);
  190. }
  191. if (!oldValue) {
  192. return value;
  193. }
  194. OrderPipe_1.setValue(value, this.transform(oldValue, lastPredicate, reverse, isCaseInsensitive), parsedExpression);
  195. return value;
  196. };
  197. /**
  198. * Apply multiple expressions
  199. *
  200. * @param value
  201. * @param {any[]} expressions
  202. * @param {boolean} reverse
  203. * @param {boolean} isCaseInsensitive
  204. * @param {Function} comparator
  205. * @returns {any}
  206. */
  207. OrderPipe.prototype.multiExpressionTransform = function (value, expressions, reverse, isCaseInsensitive, comparator) {
  208. var _this = this;
  209. if (isCaseInsensitive === void 0) { isCaseInsensitive = false; }
  210. return expressions.reverse().reduce(function (result, expression) {
  211. return _this.transform(result, expression, reverse, isCaseInsensitive, comparator);
  212. }, value);
  213. };
  214. var OrderPipe_1;
  215. OrderPipe = OrderPipe_1 = __decorate([
  216. core.Pipe({
  217. name: "orderBy",
  218. pure: false
  219. })
  220. ], OrderPipe);
  221. return OrderPipe;
  222. }());
  223. var OrderModule = /** @class */ (function () {
  224. function OrderModule() {
  225. }
  226. OrderModule = __decorate([
  227. core.NgModule({
  228. declarations: [OrderPipe],
  229. exports: [OrderPipe],
  230. providers: [OrderPipe]
  231. })
  232. ], OrderModule);
  233. return OrderModule;
  234. }());
  235. exports.OrderModule = OrderModule;
  236. exports.OrderPipe = OrderPipe;
  237. Object.defineProperty(exports, '__esModule', { value: true });
  238. }));
  239. //# sourceMappingURL=ngx-order-pipe.umd.js.map