1 | # ngx-pipes
|
2 | [![npm](https://img.shields.io/npm/v/ngx-pipes.svg?style=flat-square)](https://www.npmjs.com/package/ngx-pipes) [![Travis](https://img.shields.io/travis/danrevah/ngx-pipes.svg?style=flat-square)](https://travis-ci.org/danrevah/ngx-pipes) [![Coveralls](https://img.shields.io/coveralls/danrevah/ngx-pipes.svg?style=flat-square)](https://coveralls.io/github/danrevah/ngx-pipes?branch=master) [![license](https://img.shields.io/github/license/mashape/apistatus.svg?style=flat-square)](https://github.com/danrevah/ng-pipes/blob/master/LICENSE.md)
|
3 |
|
4 | > Useful pipes for Angular 2 and beyond with no external dependencies!
|
5 |
|
6 | ## Table of contents
|
7 |
|
8 | - [Installation](#installation)
|
9 | - [Contributing](#contributing)
|
10 | - [String](#string)
|
11 | - [repeat](#repeat)
|
12 | - [scan](#scan)
|
13 | - [shorten](#shorten)
|
14 | - [stripTags](#striptags)
|
15 | - [ucfirst](#ucfirst)
|
16 | - [ucwords](#ucwords)
|
17 | - [trim](#trim)
|
18 | - [ltrim](#ltrim)
|
19 | - [rtrim](#rtrim)
|
20 | - [reverse](#reverse)
|
21 | - [slugify](#slugify)
|
22 | - [camelize](#camelize)
|
23 | - [latinise](#latinise)
|
24 | - [lines](#lines)
|
25 | - [underscore](#underscore)
|
26 | - [test](#test)
|
27 | - [match](#match)
|
28 | - [lpad](#lpad)
|
29 | - [rpad](#rpad)
|
30 | - [Array](#Array)
|
31 | - [diff](#diff)
|
32 | - [flatten](#flatten)
|
33 | - [initial](#initial)
|
34 | - [intersection](#intersection)
|
35 | - [reverse](#reverse)
|
36 | - [tail](#tail)
|
37 | - [truthify](#truthify)
|
38 | - [union](#union)
|
39 | - [unique](#unique)
|
40 | - [without](#without)
|
41 | - [pluck](#pluck)
|
42 | - [shuffle](#shuffle)
|
43 | - [every](#every)
|
44 | - [some](#some)
|
45 | - [sample](#sample)
|
46 | - [groupBy](#groupby)
|
47 | - [filterBy](#filterby)
|
48 | - [orderBy](#orderby)
|
49 | - [Object](#object)
|
50 | - [keys](#keys)
|
51 | - [values](#values)
|
52 | - [pairs](#pairs)
|
53 | - [pick](#pick)
|
54 | - [omit](#omit)
|
55 | - [invert](#invert)
|
56 | - [invertBy](#invertby)
|
57 | - [Math](#math)
|
58 | - [min](#min)
|
59 | - [max](#max)
|
60 | - [sum](#sum)
|
61 | - [percentage](#percentage)
|
62 | - [ceil](#ceil)
|
63 | - [floor](#floor)
|
64 | - [round](#round)
|
65 | - [sqrt](#sqrt)
|
66 | - [pow](#pow)
|
67 | - [degrees](#degrees)
|
68 | - [radians](#radians)
|
69 | - [bytes](#bytes)
|
70 | - [Boolean](#boolean)
|
71 | - [isNull](#isnull)
|
72 | - [isDefined](#isdefined)
|
73 | - [isUndefined](#isundefined)
|
74 | - [isString](#isstring)
|
75 | - [isFunction](#isfunction)
|
76 | - [isNumber](#isnumber)
|
77 | - [isArray](#isarray)
|
78 | - [isObject](#isobject)
|
79 | - [isGreaterThan](#isgreaterthan)
|
80 | - [isGreaterEqualThan](#isgreaterequalthan)
|
81 | - [isLessThan](#islessthan)
|
82 | - [isLessEqualThan](#islessequalthan)
|
83 | - [isEqualTo](#isequalto)
|
84 | - [isNotEqualTo](#isnotequalto)
|
85 | - [isIdenticalTo](#isidenticalto)
|
86 | - [isNotIdenticalTo](#isnotidenticalto)
|
87 |
|
88 |
|
89 | ## Installation
|
90 |
|
91 | 1. Use npm to install the package
|
92 |
|
93 | ```terminal
|
94 | $ npm install ngx-pipes --save
|
95 | ```
|
96 |
|
97 | 2. You could either add into your module `imports` the `NgPipesModule` in order to add all of the pipes, Or add a specific module such as `NgArrayPipesModule`, `NgObjectPipesModule`, `NgStringPipesModule`, `NgMathPipesModule` or `NgBooleanPipesModule`.
|
98 |
|
99 | ```typescript
|
100 | import {NgPipesModule} from 'ngx-pipes';
|
101 |
|
102 | @NgModule({
|
103 | // ...
|
104 | imports: [
|
105 | // ...
|
106 | NgPipesModule
|
107 | ]
|
108 | })
|
109 | ```
|
110 |
|
111 | 3. Pipes are also injectable and can be used in Components / Services / etc..
|
112 |
|
113 | ```typescript
|
114 | import {ReversePipe} from 'ngx-pipes/src/app/pipes/array/reverse';
|
115 |
|
116 | @Component({
|
117 | // ..
|
118 | providers: [ReversePipe]
|
119 | })
|
120 | export class AppComponent {
|
121 | constructor(private reversePipe: ReversePipe) {
|
122 | this.reversePipe.transform('foo'); // Returns: "oof"
|
123 | }
|
124 | // ..
|
125 | }
|
126 | ```
|
127 |
|
128 |
|
129 |
|
130 |
|
131 | ## String
|
132 |
|
133 | ### repeat
|
134 |
|
135 | Repeats a string n times
|
136 |
|
137 | **Usage:** `string | repeat: times: [separator|optional]`
|
138 |
|
139 | ```html
|
140 | <p>{{ 'example' | repeat: 3: '@' }}</p> <!-- Output: "example@example@example" -->
|
141 | ```
|
142 |
|
143 | ### scan
|
144 |
|
145 | Scans string and replace `{i}` placeholders by equivalent member of the array
|
146 |
|
147 | **Usage:** `string | scan: [ARRAY]`
|
148 |
|
149 | ```html
|
150 | <p>{{'Hey {0}, {1}' | scan: ['foo', 'bar']}}</p> <!-- Output: "Hey foo, bar" -->
|
151 | ```
|
152 |
|
153 | ### shorten
|
154 |
|
155 | Shortening a string by length and providing a custom string to denote an omission
|
156 |
|
157 | **Usage:** `string | shorten: length: [suffix|optional]: [wordBreak boolean|optional]`
|
158 |
|
159 | ```html
|
160 | <p>{{'Hey foo bar' | shorten: 3: '...'}}</p> <!-- Output: "Hey..." -->
|
161 | ```
|
162 |
|
163 | ### stripTags
|
164 |
|
165 | Strips a HTML tags from string and providing which tags should not be removed
|
166 |
|
167 | **Usage:** `string | stripTags: [ARRAY]`
|
168 |
|
169 | ```html
|
170 | <p>{{'<a href="">foo</a> <p class="foo">bar</p>' | stripTags }}</p> <!-- Output: "foo bar" -->
|
171 | <p>{{'<a href="">foo</a> <p class="foo">bar</p>' | stripTags: 'p'}}</p> <!-- Output: foo <p class="foo">bar</p> -->
|
172 | ```
|
173 |
|
174 | ### ucfirst
|
175 |
|
176 | Uppercase first letter of first word
|
177 |
|
178 | ```html
|
179 | <p>{{'foo bar' | ucfirst }}</p> <!-- Output: "Foo bar" -->
|
180 | ```
|
181 |
|
182 | ### ucwords
|
183 |
|
184 | Uppercase first letter every word
|
185 |
|
186 | ```html
|
187 | <p>{{'foo bar' | ucwords }}</p> <!-- Output: "Foo Bar" -->
|
188 | ```
|
189 |
|
190 | ### trim
|
191 |
|
192 | Strips characters from the beginning and end of a string (default character is space).
|
193 |
|
194 | **Usage:** `string | trim: [characters|optional]`
|
195 |
|
196 | ```html
|
197 | <p>{{' foo ' | trim }}</p> <!-- Output: "foo" -->
|
198 | <p>{{'foobarfoo' | trim: 'foo' }}</p> <!-- Output: "bar" -->
|
199 | ```
|
200 |
|
201 | ### ltrim
|
202 |
|
203 | Strips characters from the beginning of a string (default character is space).
|
204 |
|
205 | **Usage:** `string | ltrim: [characters|optional]`
|
206 |
|
207 | ```html
|
208 | <p>{{' foo ' | ltrim }}</p> <!-- Output: "foo " -->
|
209 | <p>{{'foobarfoo' | ltrim: 'foo' }}</p> <!-- Output: "barfoo" -->
|
210 | ```
|
211 |
|
212 | ### rtrim
|
213 |
|
214 | Strips characters from the end of a string (default character is space).
|
215 |
|
216 | **Usage:** `string | rtrim: [characters|optional]`
|
217 |
|
218 | ```html
|
219 | <p>{{' foo ' | rtrim }}</p> <!-- Output: " foo" -->
|
220 | <p>{{'foobarfoo' | rtrim: 'foo' }}</p> <!-- Output: "foobar" -->
|
221 | ```
|
222 |
|
223 | ### reverse
|
224 |
|
225 | Reverses a string
|
226 |
|
227 | **Usage:** `string | reverse`
|
228 |
|
229 | ```html
|
230 | <p>{{'foo bar' | reverse }}</p> <!-- Output: "rab oof" -->
|
231 | ```
|
232 |
|
233 | ### slugify
|
234 |
|
235 | Slugify a string (lower case and add dash between words).
|
236 |
|
237 | **Usage:** `string | slugify`
|
238 |
|
239 | ```html
|
240 | <p>{{'Foo Bar' | slugify }}</p> <!-- Output: "foo-bar" -->
|
241 | <p>{{'Some Text To Slugify' | slugify }}</p> <!-- Output: "some-text-to-slugify" -->
|
242 | ```
|
243 |
|
244 | ### camelize
|
245 |
|
246 | Camelize a string replaces dashes and underscores and converts to camelCase string.
|
247 |
|
248 | **Usage:** `string | camelize`
|
249 |
|
250 | ```html
|
251 | <p>{{'foo_bar' | camelize }}</p> <!-- Output: "fooBar" -->
|
252 | <p>{{'some_dashed-with-underscore' | camelize }}</p> <!-- Output: "someDashedWithUnderscore" -->
|
253 | <p>{{'-dash_first-' | camelize }}</p> <!-- Output: "dashFirst" -->
|
254 | ```
|
255 |
|
256 | ### latinise
|
257 |
|
258 | Removes accents from Latin characters.
|
259 |
|
260 | **Usage:** `string | latinise`
|
261 |
|
262 | ```html
|
263 | <p>{{'Féé' | latinise }}</p> <!-- Output: "Fee" -->
|
264 | <p>{{'foo' | latinise }}</p> <!-- Output: "foo" -->
|
265 | ```
|
266 |
|
267 | ### lines
|
268 |
|
269 | Converts a string with new lines into an array of each line.
|
270 |
|
271 | **Usage:** `string | lines`
|
272 |
|
273 | ```html
|
274 | <p>{{'Some\nSentence with\r\nNew line' | lines }}</p> <!-- Output: "['Some', 'Sentence with', 'New line']" -->
|
275 | ```
|
276 |
|
277 | ### underscore
|
278 |
|
279 | Converts camelCase string to underscore.
|
280 |
|
281 | **Usage:** `string | underscore`
|
282 |
|
283 | ```html
|
284 | <p>{{'angularIsAwesome' | underscore }}</p> <!-- Output: "angular_is_awesome" -->
|
285 | <p>{{'FooBar' | underscore }}</p> <!-- Output: "foo_bar" -->
|
286 | ```
|
287 |
|
288 | ### test
|
289 |
|
290 | Tests if a string matches a pattern.
|
291 |
|
292 | **Usage:** `string | test: {RegExp}: {Flags}`
|
293 |
|
294 | ```html
|
295 | <p>{{'foo 42' | test: '[\\d]+$': 'g' }}</p> <!-- Output: true -->
|
296 | <p>{{'42 foo' | test: '[\\d]+$': 'g' }}</p> <!-- Output: false -->
|
297 | <p>{{'FOO' | test: '^foo': 'i' }}</p> <!-- Output: true -->
|
298 | ```
|
299 |
|
300 | ### match
|
301 |
|
302 | Returns array of matched elements in string.
|
303 |
|
304 | **Usage:** `string | match: {RegExp}: {Flags}`
|
305 |
|
306 | ```html
|
307 | <p>{{'foo 42' | match: '[\\d]+$': 'g' }}</p> <!-- Output: '42' -->
|
308 | <p>{{'42 foo' | match: '[\\d]+$': 'g' }}</p> <!-- Output: null -->
|
309 | <p>{{'FOO' | match: '^foo': 'i' }}</p> <!-- Output: 'FOO' -->
|
310 | ```
|
311 |
|
312 | ### lpad
|
313 |
|
314 | Left pad a string to a given length using a given pad character (default is a space)
|
315 |
|
316 |
|
317 | **Usage:** `string | lpad: length: [padCharacter:string|optional]`
|
318 |
|
319 | ```html
|
320 | <p>{{'foo' | lpad: 5}}</p> <!-- Output: " foo" -->
|
321 | <!-- Cast a number to string in order to left pad it with zeros -->
|
322 | <p>{{String(3) | lpad: 5: '0'}}</p> <!-- Output: "00003" -->
|
323 | ```
|
324 |
|
325 | ### rpad
|
326 |
|
327 | Right pad a string to a given length using a given pad character (default is a space)
|
328 |
|
329 |
|
330 | **Usage:** `string | rpad: length: [padCharacter:string|optional]`
|
331 |
|
332 | ```html
|
333 | <p>{{'Foo' | rpad: 5: '#'}}</p> <!-- Output: "Foo##" -->
|
334 | ```
|
335 |
|
336 | ## Array
|
337 |
|
338 | ### diff
|
339 |
|
340 | Returns array of diff between arrays
|
341 |
|
342 | **Usage:** `array | diff: [ARRAY]: [ARRAY]: ... : [ARRAY]`
|
343 |
|
344 | ```typescript
|
345 | this.items = [1, 2, 3, 4];
|
346 | ```
|
347 |
|
348 | ```html
|
349 | <li *ngFor="let item of items | diff: [1, 2]"> <!-- Array: [3, 4] -->
|
350 | ```
|
351 |
|
352 | ### flatten
|
353 |
|
354 | Flattens nested array, passing shallow will mean it will only be flattened a single level
|
355 |
|
356 | **Usage:** `array | flatten: [shallow|optional]`
|
357 |
|
358 | ```typescript
|
359 | this.items = [1,2,3,[4,5,6,[7,8,9],[10,11,12,13,[14],[15],[16, [17]]]]];
|
360 | ```
|
361 |
|
362 | ```html
|
363 | <li *ngFor="let item of items | flatten">
|
364 | <!-- Array: [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17] -->
|
365 | ```
|
366 |
|
367 | ### initial
|
368 |
|
369 | Slicing off the end of the array by n elements
|
370 |
|
371 | **Usage:** `array | initial: n`
|
372 |
|
373 | ```typescript
|
374 | this.items = [first, second, third];
|
375 | ```
|
376 |
|
377 | ```html
|
378 | <li *ngFor="let item of items | initial: 1"> <!-- Array: [first, second] -->
|
379 | ```
|
380 |
|
381 | ### tail
|
382 |
|
383 | Slicing off the start of the array by n elements
|
384 |
|
385 | **Usage:** `array | tail: n`
|
386 |
|
387 | ```typescript
|
388 | this.items = [first, second, third];
|
389 | ```
|
390 |
|
391 | ```html
|
392 | <li *ngFor="let item of items | tail: 1"> <!-- Array: [second, third] -->
|
393 | ```
|
394 |
|
395 | ### intersection
|
396 |
|
397 | Returns the intersections of an arrays
|
398 |
|
399 | **Usage:** `array | intersection: [ARRAY]: [ARRAY]: ... : [ARRAY]`
|
400 |
|
401 | ```typescript
|
402 | this.items = [1, 2, 3, 4, 5];
|
403 | ```
|
404 |
|
405 | ```html
|
406 | <li *ngFor="let item of items | intersection: [1, 2, 3]: [3, 6]"> <!-- Array: [3] -->
|
407 | ```
|
408 |
|
409 | ### reverse
|
410 |
|
411 | Reverses an array
|
412 |
|
413 | **Usage:** `array | reverse`
|
414 |
|
415 | ```typescript
|
416 | this.items = [1, 2, 3];
|
417 | ```
|
418 |
|
419 | ```html
|
420 | <li *ngFor="let item of items | reverse"> <!-- Array: [3, 2, 1] -->
|
421 | ```
|
422 |
|
423 | ### truthify
|
424 |
|
425 | Removes un-truthy values from array
|
426 |
|
427 | **Usage:** `array | truthify`
|
428 |
|
429 | ```typescript
|
430 | this.items = [null, 1, false, undefined, 2, 0, 3, NaN, 4, ''];
|
431 | ```
|
432 |
|
433 | ```html
|
434 | <li *ngFor="let item of items | truthify"> <!-- Array: [1, 2, 3, 4] -->
|
435 | ```
|
436 |
|
437 | ### union
|
438 |
|
439 | Removes un-truthy values from array
|
440 |
|
441 | **Usage:** `array | union: [ARRAY]`
|
442 |
|
443 | ```typescript
|
444 | this.items = [1, 2, 3];
|
445 | ```
|
446 |
|
447 | ```html
|
448 | <li *ngFor="let item of items | union: [[4]]"> <!-- Array: [1, 2, 3, 4] -->
|
449 | ```
|
450 |
|
451 | ### unique
|
452 |
|
453 | Removes duplicates from array
|
454 |
|
455 | **Usage:** `array | unique`
|
456 |
|
457 | ```typescript
|
458 | this.items = [1, 2, 3, 1, 2, 3];
|
459 | ```
|
460 |
|
461 | ```html
|
462 | <li *ngFor="let item of items | unique"> <!-- Array: [1, 2, 3] -->
|
463 | ```
|
464 |
|
465 | ### without
|
466 |
|
467 | Returns array without specific elements
|
468 |
|
469 | **Usage:** `array | without: [ARRAY]`
|
470 |
|
471 | ```typescript
|
472 | this.items = [1, 2, 3, 1, 2, 3];
|
473 | ```
|
474 |
|
475 | ```html
|
476 | <li *ngFor="let item of items | without: [1,3]"> <!-- Array: [2, 2] -->
|
477 | ```
|
478 |
|
479 | ### pluck
|
480 |
|
481 | Returns array of properties values
|
482 |
|
483 | **Usage:** `array | pluck: propertyName`
|
484 |
|
485 | ```typescript
|
486 | this.items = [
|
487 | {
|
488 | a: 1,
|
489 | b: {
|
490 | c: 4
|
491 | }
|
492 | },
|
493 | {
|
494 | a: 2,
|
495 | b: {
|
496 | c: 5
|
497 | }
|
498 | },
|
499 | {
|
500 | a: 3,
|
501 | b: {
|
502 | c: 6
|
503 | }
|
504 | },
|
505 | ];
|
506 | ```
|
507 |
|
508 | ```html
|
509 | <li *ngFor="let item of items | pluck: 'a'"> <!-- Array: [1, 2, 3] -->
|
510 | <li *ngFor="let item of items | pluck: 'b.c'"> <!-- Array: [4, 5, 6] -->
|
511 | ```
|
512 |
|
513 | ### shuffle
|
514 |
|
515 | Returns randomly shuffled array
|
516 |
|
517 | **Usage:** `array | shuffle`
|
518 |
|
519 | ```typescript
|
520 | this.items = [1, 2, 3, 4, 5, 6];
|
521 | ```
|
522 |
|
523 | ```html
|
524 | <li *ngFor="let item of items | shuffle"> <!-- Array: [4, 1, 6, 2, 5, 3] -->
|
525 | ```
|
526 |
|
527 | ### every
|
528 |
|
529 | Returns true if every elements of the array fits the predicate otherwise false
|
530 |
|
531 | **Usage:** `array | every: predicate`
|
532 |
|
533 | ```typescript
|
534 | this.itemsOne = [1, 1, 1];
|
535 | this.itemsTwo = [1, 1, 2];
|
536 | this.itemsThree = [2, 2, 2];
|
537 | this.predicate = (value: any, index: number, array: any[]): boolean => {
|
538 | return value === 1;
|
539 | };
|
540 | ```
|
541 |
|
542 | ```html
|
543 | <p>{{ itemsOne | every: predicate }}</p> <!-- Output: "true" -->
|
544 | <p>{{ itemsTwo | every: predicate }}</p> <!-- Output: "false" -->
|
545 | <p>{{ itemsThree | every: predicate }}</p> <!-- Output: "false" -->
|
546 | ```
|
547 |
|
548 | ### some
|
549 |
|
550 | Returns true if some elements of the array fits the predicate otherwise false
|
551 |
|
552 | **Usage:** `array | some: predicate`
|
553 |
|
554 | ```typescript
|
555 | this.itemsOne = [1, 1, 1];
|
556 | this.itemsTwo = [1, 1, 2];
|
557 | this.itemsThree = [2, 2, 2];
|
558 | this.predicate = (value: any, index: number, array: any[]): boolean => {
|
559 | return value === 1;
|
560 | };
|
561 | ```
|
562 |
|
563 | ```html
|
564 | <p>{{ itemsOne | some: predicate }}</p> <!-- Output: "true" -->
|
565 | <p>{{ itemsTwo | some: predicate }}</p> <!-- Output: "true" -->
|
566 | <p>{{ itemsThree | some: predicate }}</p> <!-- Output: "false" -->
|
567 | ```
|
568 |
|
569 | ### sample
|
570 |
|
571 | Returns sample items randomly from array
|
572 |
|
573 | **Usage:** `array | sample: [amount | default = 1]`
|
574 |
|
575 | ```html
|
576 | <p>{{ [1, 2, 3, 4] | sample }}</p> <!-- Output: "[2]" -->
|
577 | <p>{{ [1, 2, 3, 4] | sample: 2 }}</p> <!-- Output: "[4, 3]" -->
|
578 | ```
|
579 |
|
580 | ### groupBy
|
581 |
|
582 | Returns object of grouped by items by discriminator, and supports nested properties.
|
583 |
|
584 | **Usage:** `array | groupBy: [string[] | string | Function]`
|
585 |
|
586 | ```typescript
|
587 | this.arrayObject = [
|
588 | {id: 1, elm: 'foo', value: 0},
|
589 | {id: 2, elm: 'bar', value: 1},
|
590 | {id: 3, elm: 'foo', value: 2},
|
591 | {id: 4, elm: 'foo', value: 2}
|
592 | ];
|
593 |
|
594 | this.arrayNestedObject = [
|
595 | {id: 1, prop: { deep: 'foo' }},
|
596 | {id: 2, prop: { deep: 'bar' }},
|
597 | {id: 3, prop: { deep: 'foo' }},
|
598 | {id: 4, prop: { deep: 'bar' }}
|
599 | ];
|
600 | ```
|
601 |
|
602 | ```html
|
603 | <p>{{ arrayObject | groupBy: 'elm' }}</p>
|
604 | <!-- Output: "{foo: [{id: 1, elm: 'foo', value: 0}, {id: 3, elm: 'foo', value: 2}, {id: 4, elm: 'foo', value: 2}], bar: [{id: 2, elm: 'bar', value: 1}]}" -->
|
605 |
|
606 | <p>{{ arrayObject | groupBy: ['elm', 'value'] }}</p>
|
607 | <!-- Output: "{foo_0: [{elm: foo, value: 0}], bar_1: [{elm:bar,value: 1}], foo_2: [{elm:foo, value: 2}], bar_3: [{elm:bar, value: 3}]}" -->
|
608 |
|
609 | <p>{{ arrayNestedObject | groupBy: 'prop.deep' }}</p>
|
610 | <!-- Output:{foo: [{id: 1, prop: {deep: foo}}, {id: 3, prop: {deep: foo}}], bar: [{id: 2, prop: {deep: bar}}, {id: 4, prop: {deep: bar}}]}" -->
|
611 | ```
|
612 |
|
613 | ### filterBy
|
614 |
|
615 | Returns object array of grouped by items by discriminator
|
616 |
|
617 | **Usage:** `array | filterBy: [prop, nested.prop, ...]: search: [strict | optional]`
|
618 |
|
619 | ```typescript
|
620 | this.users = [
|
621 | {id: 1, first_name: 'John', last_name: 'Doe', work: { company: 'Foo Tech' }},
|
622 | {id: 2, first_name: 'Jane', last_name: 'West', work: { company: 'AAA Solutions' }},
|
623 | {id: 3, first_name: 'Bruce', last_name: 'John', work: { company: 'Bar Tech' }},
|
624 | {id: 4, first_name: 'William', last_name: 'Cent', work: { company: 'Foo Tech' }}
|
625 | ];
|
626 | ```
|
627 |
|
628 | ```html
|
629 | <!-- Returns users with `id` of 1 -->
|
630 | <p>{{ users | filterBy: ['id']: 1 }}</p>
|
631 | <!-- Output: "[{id: 1, first_name: 'John', last_name: 'Doe', work: { company: 'Foo Tech', previous_company: '' }}]" -->
|
632 |
|
633 | <!-- filterBy also support nested properties -->
|
634 | <p>{{ users | filterBy: ['work.company']: 'Bar Tech' }}</p>
|
635 | <!-- Output: "[{ "id": 3, "first_name": "Bruce", "last_name": "John", "work": { "company": "Bar Tech", "previous_company": "" } }]" -->
|
636 |
|
637 | <!-- Return users whose first name or last name is 'John'. -->
|
638 | <p>{{ users | filterBy: ['first_name', 'last_name']: 'John' }}</p>
|
639 | <!-- Output: "[{id: 1, first_name: 'John', last_name: 'Doe', work: { company: 'Foo Tech', previous_company: '' }}]" -->
|
640 | ```
|
641 |
|
642 | ### orderBy
|
643 |
|
644 | Returns ordered array by configuration
|
645 |
|
646 | **Usage:** `array | orderBy: [prop, nested.prop, array of props, ...]`
|
647 |
|
648 | ```typescript
|
649 | const numbers = [2, 1, 3];
|
650 |
|
651 | const obj = [
|
652 | {id: 4, name: 'Dave', amount: 2},
|
653 | {id: 2, name: 'Michael', amount: 2},
|
654 | {id: 3, name: 'Dan', amount: 1},
|
655 | {id: 1, name: 'John', amount: 1}
|
656 | ];
|
657 |
|
658 | const deepObj = [
|
659 | {id: 1, name: 'John', amount: 1337, deep: {prop: 4}},
|
660 | {id: 2, name: 'Michael', amount: 42, deep: {prop: 2}},
|
661 | {id: 3, name: 'Dan', amount: 1, deep: {prop: 1}},
|
662 | {id: 4, name: 'Dave', amount: 2, deep: {prop: 3}}
|
663 | ];
|
664 | ```
|
665 |
|
666 | ```html
|
667 | <!-- Returns array ordered by value -->
|
668 | <p>{{ numbers | orderBy }}</p> <!-- Output: [1, 2, 3] -->
|
669 | <p>{{ numbers | orderBy: '-' }}</p> <!-- Output: [3, 2, 1] -->
|
670 |
|
671 | <!-- Returns array ordered by value of property -->
|
672 | <p>{{ deepObj | orderBy: 'amount' }}</p>
|
673 | <!-- Output: [{id: 3, ...}, {id: 4, ...}, {id: 2, ...}, {id: 1, ...}] -->
|
674 | <p>{{ deepObj | orderBy: '-amount' }}</p>
|
675 | <!-- Output: [{id: 1, ...}, {id: 2, ...}, {id: 4, ...}, {id: 3, ...}] -->
|
676 |
|
677 | <!-- Returns array ordered by value of deep property -->
|
678 | <p>{{ deepObj | orderBy: 'deep.prop' }}</p>
|
679 | <!-- Output: [{id: 3, ...}, {id: 2, ...}, {id: 4, ...}, {id: 1, ...}] -->
|
680 | <p>{{ deepObj | orderBy: '-deep.prop' }}</p>
|
681 | <!-- Output: [{id: 1, ...}, {id: 4, ...}, {id: 2, ...}, {id: 3, ...}] -->
|
682 |
|
683 | <!-- Returns array ordered by mutliple properties -->
|
684 | <p>{{ obj | orderBy: ['amount', 'id'] }}</p>
|
685 | <!-- Output: [{id: 1, ...}, {id: 3, ...}, {id: 2, ...}, {id: 4, ...}] -->
|
686 | ```
|
687 |
|
688 | ## Object
|
689 |
|
690 | ### keys
|
691 |
|
692 | Returns array of object keys
|
693 |
|
694 | **Usage:** `object | keys`
|
695 |
|
696 | ```html
|
697 | <p>{{ {foo: 1, bar: 2} | keys }}</p> <!-- Output: "['foo', 'bar']" -->
|
698 | ```
|
699 |
|
700 | ### values
|
701 |
|
702 | Returns array of object values
|
703 |
|
704 | **Usage:** `object | values`
|
705 |
|
706 | ```html
|
707 | <p>{{ {foo: 1, bar: 2} | values }}</p> <!-- Output: "[1, 2]" -->
|
708 | ```
|
709 |
|
710 | ### pairs
|
711 |
|
712 | Returns array of an object key value pairs
|
713 |
|
714 | **Usage:** `object | pairs`
|
715 |
|
716 | ```html
|
717 | <p>{{ {foo: 1, bar: 2} | pairs }}</p> <!-- Output: "[['foo', 1], ['bar', 2]]" -->
|
718 | <p>{{ {foo: [1, 2], bar: [3, 4]} | pairs }}</p> <!-- Output: "[['foo', [1, 2]], ['bar', [3, 4]]]" -->
|
719 | ```
|
720 |
|
721 | ### pick
|
722 |
|
723 | Returns object with picked keys from object
|
724 |
|
725 | **Usage:** `object | pick: [key | string]]`
|
726 |
|
727 | ```html
|
728 | <p>{{ {foo: 1, bar: 2} | pick: 'foo' }}</p> <!-- Output: "{foo: 1}" -->
|
729 | <p>{{ {foo: 1, bar: 2} | pick: 'foo': 'bar' }}</p> <!-- Output: "{foo: 1, bar: 2}" -->
|
730 | ```
|
731 |
|
732 | ### omit
|
733 |
|
734 | Returns object after omitting keys from object (opposite of pick)
|
735 |
|
736 | **Usage:** `object | omit: [key | string]]`
|
737 |
|
738 | ```html
|
739 | <p>{{ {foo: 1, bar: 2} | omit: 'foo' }}</p> <!-- Output: "{bar: 2}" -->
|
740 | <p>{{ {foo: 1, bar: 2} | omit: 'foo': 'bar' }}</p> <!-- Output: "{}" -->
|
741 | ```
|
742 |
|
743 | ### invert
|
744 |
|
745 | Returns object with inverted keys and values. in case of equal values, subsequent values overwrite property assignments of previous values.
|
746 |
|
747 | **Usage:** `object | invert`
|
748 |
|
749 | ```html
|
750 | <p>{{ {foo: 1, bar: 2} | invert }}</p> <!-- Output: "{1: 'foo', 2: 'bar'}" -->
|
751 | ```
|
752 |
|
753 | ### invertBy
|
754 |
|
755 | Returns object with inverted keys and values. in case of equal values, will add to an array.
|
756 |
|
757 | **Usage:** `object | invertBy: [Function | optional]`
|
758 |
|
759 | ```typescript
|
760 | this.cb = (value): string => {
|
761 | return `name_${value}`;
|
762 | };
|
763 | ```
|
764 |
|
765 | ```html
|
766 | <p>{{ {foo: 1, bar: 2} | invertBy }}</p> <!-- Output: "{1: ['foo'], 2: ['bar']}" -->
|
767 | <p>{{ {foo: 1, bar: 2} | invertBy: cb }}</p> <!-- Output: "{name_1: ['foo'], name_2: ['bar']}" -->
|
768 | <p>{{ {a: 1, b: 2, c: 1, d: 2} | invertBy }}</p> <!-- Output: "{1: ['a', 'c'], 2: ['b', 'd']}" -->
|
769 | ```
|
770 |
|
771 | ## Math
|
772 |
|
773 | ### min
|
774 |
|
775 | Returns the minimum of a given array
|
776 |
|
777 | **Usage:** `array | min`
|
778 |
|
779 | ```html
|
780 | <p>{{ [1, 2, 3, 1, 2, 3] | min }}</p> <!-- Output: "1" -->
|
781 | ```
|
782 |
|
783 | ### max
|
784 |
|
785 | Returns the maximum of a given array
|
786 |
|
787 | **Usage:** `array | max`
|
788 |
|
789 | ```html
|
790 | <p>{{ [1, 2, 3, 1, 2, 3] | max }}</p> <!-- Output: "3" -->
|
791 | ```
|
792 |
|
793 | ### sum
|
794 |
|
795 | Returns the sum of a given array
|
796 |
|
797 | **Usage:** `array | sum`
|
798 |
|
799 | ```html
|
800 | <p>{{ [1, 2, 3, 4] | sum }}</p> <!-- Output: "10" -->
|
801 | ```
|
802 |
|
803 | ### percentage
|
804 |
|
805 | Returns percentage between numbers
|
806 |
|
807 | **Usage:** `number | percentage: [total | default = 100]: [floor | default = false]`
|
808 |
|
809 | ```html
|
810 | <p>{{ 5 | percentage }}</p> <!-- Output: "5" -->
|
811 | <p>{{ 5 | percentage: 160 }}</p> <!-- Output: "3.125" -->
|
812 | <p>{{ 5 | percentage: 160: true }}</p> <!-- Output: "3" -->
|
813 | ```
|
814 |
|
815 | ### ceil
|
816 |
|
817 | Returns ceil of a number by precision
|
818 |
|
819 | **Usage:** `number | ceil: [precision | default = 0]`
|
820 |
|
821 | ```html
|
822 | <p>{{ 42.123 | ceil }}</p> <!-- Output: "43" -->
|
823 | <p>{{ 42.123 | ceil: 2 }}</p> <!-- Output: "42.13" -->
|
824 | ```
|
825 |
|
826 | ### floor
|
827 |
|
828 | Returns floor of a number by precision
|
829 |
|
830 | **Usage:** `number | floor: [precision | default = 0]`
|
831 |
|
832 | ```html
|
833 | <p>{{ 42.123 | floor }}</p> <!-- Output: "42" -->
|
834 | <p>{{ 42.123 | floor: 2 }}</p> <!-- Output: "42.12" -->
|
835 | ```
|
836 |
|
837 | ### round
|
838 |
|
839 | Returns round of a number by precision
|
840 |
|
841 | **Usage:** `number | round: [precision | default = 0]`
|
842 |
|
843 | ```html
|
844 | <p>{{ 42.4 | round }}</p> <!-- Output: "42" -->
|
845 | <p>{{ 42.5 | round }}</p> <!-- Output: "43" -->
|
846 | <p>{{ 42.123 | round: 2 }}</p> <!-- Output: "42.12" -->
|
847 | ```
|
848 |
|
849 | ### sqrt
|
850 |
|
851 | Returns the square root of a number
|
852 |
|
853 | **Usage:** `number | sqrt`
|
854 |
|
855 | ```html
|
856 | <p>{{ 9 | sqrt }}</p> <!-- Output: "3" -->
|
857 | ```
|
858 |
|
859 | ### pow
|
860 |
|
861 | Returns the power of a number
|
862 |
|
863 | **Usage:** `number | pow: [power | default = 2]`
|
864 |
|
865 | ```html
|
866 | <p>{{ 3 | pow }}</p> <!-- Output: "9" -->
|
867 | <p>{{ 3 | pow: 3 }}</p> <!-- Output: "27" -->
|
868 | ```
|
869 |
|
870 | ### degrees
|
871 |
|
872 | Returns the degrees of a number in radians
|
873 |
|
874 | **Usage:** `number | degrees`
|
875 |
|
876 | ```html
|
877 | <p>{{ 3.141592653589793 | degrees }}</p> <!-- Output: "180" -->
|
878 | ```
|
879 |
|
880 | ### radians
|
881 |
|
882 | Returns the radians of a number in degrees
|
883 |
|
884 | **Usage:** `number | radians`
|
885 |
|
886 | ```html
|
887 | <p>{{ 180 | radians }}</p> <!-- Output: "3.141592653589793" -->
|
888 | ```
|
889 |
|
890 | ### bytes
|
891 |
|
892 | Returns bytes with a unit symbol
|
893 |
|
894 | **Usage:** `number | bytes`
|
895 |
|
896 | ```html
|
897 | <p>{{ 10 | bytes }}</p> <!-- Output: "10 B" -->
|
898 | <p>{{ 1000 | bytes }}</p> <!-- Output: "1 KB" -->
|
899 | <p>{{ 1000000 | bytes }}</p> <!-- Output: "1 MB" -->
|
900 | <p>{{ 1000000000 | bytes }}</p> <!-- Output: "1 GB" -->
|
901 | ```
|
902 |
|
903 | ## Boolean
|
904 |
|
905 | ### isNull
|
906 |
|
907 | **Usage:** `any | isNull`
|
908 |
|
909 | ```html
|
910 | <p>{{ null | isNull }}</p> <!-- Output: "true" -->
|
911 | <p>{{ 1 | isNull }}</p> <!-- Output: "false" -->
|
912 | ```
|
913 |
|
914 | ### isDefined
|
915 |
|
916 | **Usage:** `any | isDefined`
|
917 |
|
918 | ```html
|
919 | <p>{{ 1 | isDefined }}</p> <!-- Output: "true" -->
|
920 | <p>{{ undefined | isDefined }}</p> <!-- Output: "false" -->
|
921 | ```
|
922 |
|
923 | ### isUndefined
|
924 |
|
925 | **Usage:** `any | isUndefined`
|
926 |
|
927 | ```html
|
928 | <p>{{ 1 | isUndefined }}</p> <!-- Output: "false" -->
|
929 | <p>{{ undefined | isUndefined }}</p> <!-- Output: "true" -->
|
930 | ```
|
931 |
|
932 |
|
933 | ### isString
|
934 |
|
935 | **Usage:** `any | isString`
|
936 |
|
937 | ```html
|
938 | <p>{{ 1 | isString }}</p> <!-- Output: "false" -->
|
939 | <p>{{ '' | isString }}</p> <!-- Output: "true" -->
|
940 | ```
|
941 |
|
942 | ### isNumber
|
943 |
|
944 | **Usage:** `any | isNumber`
|
945 |
|
946 | ```typescript
|
947 | this.func = () => {};
|
948 | this.num = 1;
|
949 | ```
|
950 |
|
951 | ```html
|
952 | <p>{{ num | isNumber }}</p> <!-- Output: "true" -->
|
953 | <p>{{ func | isNumber }}</p> <!-- Output: "false" -->
|
954 | ```
|
955 |
|
956 | ### isArray
|
957 |
|
958 | **Usage:** `any | isArray`
|
959 |
|
960 | ```typescript
|
961 | this.arr = [1, 2];
|
962 | this.num = 1;
|
963 | ```
|
964 |
|
965 | ```html
|
966 | <p>{{ num | isArray }}</p> <!-- Output: "false" -->
|
967 | <p>{{ arr | isArray }}</p> <!-- Output: "true" -->
|
968 | ```
|
969 |
|
970 | ### isObject
|
971 |
|
972 | **Usage:** `any | isObject`
|
973 |
|
974 | ```typescript
|
975 | this.obj = {a: 1, b: 2};
|
976 | this.num = 1;
|
977 | ```
|
978 |
|
979 | ```html
|
980 | <p>{{ num | isObject }}</p> <!-- Output: "false" -->
|
981 | <p>{{ obj | isObject }}</p> <!-- Output: "true" -->
|
982 | ```
|
983 |
|
984 | ### isGreaterThan
|
985 |
|
986 | **Usage:** `number | isGreaterThan: otherNumber`
|
987 |
|
988 | ```html
|
989 | <p>{{ 1 | isGreaterThan: 1 }}</p> <!-- Output: "false" -->
|
990 | <p>{{ 1 | isGreaterThan: 2 }}</p> <!-- Output: "false" -->
|
991 | <p>{{ 2 | isGreaterThan: 1 }}</p> <!-- Output: "true" -->
|
992 | ```
|
993 |
|
994 | ### isGreaterEqualThan
|
995 |
|
996 | **Usage:** `number | isGreaterEqualThan: otherNumber`
|
997 |
|
998 | ```html
|
999 | <p>{{ 1 | isGreaterEqualThan: 1 }}</p> <!-- Output: "true" -->
|
1000 | <p>{{ 1 | isGreaterEqualThan: 2 }}</p> <!-- Output: "false" -->
|
1001 | <p>{{ 2 | isGreaterEqualThan: 1 }}</p> <!-- Output: "true" -->
|
1002 | ```
|
1003 |
|
1004 | ### isLessThan
|
1005 |
|
1006 | **Usage:** `number | isLessThan: otherNumber`
|
1007 |
|
1008 | ```html
|
1009 | <p>{{ 1 | isLessThan: 1 }}</p> <!-- Output: "false" -->
|
1010 | <p>{{ 1 | isLessThan: 2 }}</p> <!-- Output: "true" -->
|
1011 | <p>{{ 2 | isLessThan: 1 }}</p> <!-- Output: "false" -->
|
1012 | ```
|
1013 |
|
1014 | ### isLessEqualThan
|
1015 |
|
1016 | **Usage:** `number | isLessEqualThan: otherNumber`
|
1017 |
|
1018 | ```html
|
1019 | <p>{{ 1 | isLessEqualThan: 1 }}</p> <!-- Output: "true" -->
|
1020 | <p>{{ 1 | isLessEqualThan: 2 }}</p> <!-- Output: "true" -->
|
1021 | <p>{{ 2 | isLessEqualThan: 1 }}</p> <!-- Output: "false" -->
|
1022 | ```
|
1023 |
|
1024 | ### isEqualTo
|
1025 |
|
1026 | **Usage:** `number | isEqualTo: otherNumber`
|
1027 |
|
1028 | ```html
|
1029 | <p>{{ 1 | isEqualTo: 1 }}</p> <!-- Output: "true" -->
|
1030 | <p>{{ 1 | isEqualTo: '1' }}</p> <!-- Output: "true" -->
|
1031 | <p>{{ 1 | isEqualTo: 2 }}</p> <!-- Output: "false" -->
|
1032 | <p>{{ 2 | isEqualTo: 1 }}</p> <!-- Output: "false" -->
|
1033 | ```
|
1034 |
|
1035 | ### isNotEqualTo
|
1036 |
|
1037 | **Usage:** `number | isNotEqualTo: otherNumber`
|
1038 |
|
1039 | ```html
|
1040 | <p>{{ 1 | isNotEqualTo: 1 }}</p> <!-- Output: "false" -->
|
1041 | <p>{{ 1 | isNotEqualTo: '1' }}</p> <!-- Output: "false" -->
|
1042 | <p>{{ 1 | isNotEqualTo: 2 }}</p> <!-- Output: "true" -->
|
1043 | <p>{{ 2 | isNotEqualTo: 1 }}</p> <!-- Output: "true" -->
|
1044 | ```
|
1045 |
|
1046 | ### isIdenticalTo
|
1047 |
|
1048 | **Usage:** `number | isIdenticalTo: otherNumber`
|
1049 |
|
1050 | ```html
|
1051 | <p>{{ 1 | isIdenticalTo: 1 }}</p> <!-- Output: "true" -->
|
1052 | <p>{{ 1 | isIdenticalTo: '1' }}</p> <!-- Output: "false" -->
|
1053 | <p>{{ 1 | isIdenticalTo: 2 }}</p> <!-- Output: "false" -->
|
1054 | <p>{{ 2 | isIdenticalTo: 1 }}</p> <!-- Output: "false" -->
|
1055 | ```
|
1056 |
|
1057 | ### isNotIdenticalTo
|
1058 |
|
1059 | **Usage:** `number | isNotIdenticalTo: otherNumber`
|
1060 |
|
1061 | ```html
|
1062 | <p>{{ 1 | isNotIdenticalTo: 1 }}</p> <!-- Output: "false" -->
|
1063 | <p>{{ 1 | isNotIdenticalTo: '1' }}</p> <!-- Output: "true" -->
|
1064 | <p>{{ 1 | isNotIdenticalTo: 2 }}</p> <!-- Output: "true" -->
|
1065 | <p>{{ 2 | isNotIdenticalTo: 1 }}</p> <!-- Output: "true" -->
|
1066 | ```
|
1067 |
|
1068 | ## Contributing
|
1069 |
|
1070 | * Before adding any new feature or a fix make sure to open an issue first!
|
1071 |
|
1072 | Make sure you have `angular-cli` & `karma` installed globally.
|
1073 |
|
1074 | ```bash
|
1075 | $ npm install -g angular-cli karma
|
1076 | ```
|
1077 |
|
1078 | Clone the project, and install dependencies.
|
1079 |
|
1080 | ```bash
|
1081 | $ git clone https://github.com/danrevah/ng2-pipes.git
|
1082 | $ npm install
|
1083 | ```
|
1084 |
|
1085 | Create a new branch
|
1086 |
|
1087 | ```bash
|
1088 | $ git checkout -b feat/someFeature
|
1089 | ```
|
1090 |
|
1091 | Add tests & make sure everything is running properly
|
1092 | ```bash
|
1093 | $ npm test
|
1094 | ```
|
1095 |
|
1096 | Commit & push, and make a pull request!
|