UNPKG

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