Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
danrevah committed Jan 30, 2017
1 parent 022f42d commit 4c4329f
Show file tree
Hide file tree
Showing 35 changed files with 138 additions and 126 deletions.
1 change: 1 addition & 0 deletions src/app/pipes/array/diff.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ describe('DiffPipe', () => {
});

it('should returns an array of diff between arrays', () => {
expect(pipe.transform([2, 3], [])).toEqual([2, 3]);
expect(pipe.transform([], [2, 3])).toEqual([]);
expect(pipe.transform([1, 2, 3], [2, 3])).toEqual([1]);
expect(pipe.transform([1, 2, 3], [1, 4], [2, 5])).toEqual([3]);
Expand Down
10 changes: 6 additions & 4 deletions src/app/pipes/array/diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import {PipeTransform, Pipe} from '@angular/core';
@Pipe({name: 'diff'})
export class DiffPipe implements PipeTransform {

transform(arr: any, ...args: any[]): any[] {
return !Array.isArray(arr) ? arr : args.reduce((diffArr, currArr) => {
return diffArr.filter(elm => !~currArr.indexOf(elm))
}, arr);
transform(input: any, ...args: any[]): any[] {
if (!Array.isArray(input)) {
return input;
}

return args.reduce((d, c) => d.filter(e => !~c.indexOf(e)), input);
}
}
4 changes: 2 additions & 2 deletions src/app/pipes/array/every.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {PipeTransform, Pipe} from '@angular/core';
@Pipe({name: 'every'})
export class EveryPipe implements PipeTransform {

transform(arr: any[], predicate: (value: any, index: number, array: any[]) => boolean): boolean|any[] {
return Array.isArray(arr) ? arr.every(predicate) : arr;
transform(input: any[], predicate: (value: any, index: number, array: any[]) => boolean): boolean|any[] {
return Array.isArray(input) ? input.every(predicate) : input;
}
}
16 changes: 8 additions & 8 deletions src/app/pipes/array/filter-by.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import {PipeTransform, Pipe} from '@angular/core';
import {isNumber, isString, extractDeepPropertyByMapKey} from '../helpers/helpers';
import {isString, extractDeepPropertyByMapKey, isNumberFinite} from '../helpers/helpers';

@Pipe({name: 'filterBy'})
export class FilterByPipe implements PipeTransform {

transform(arr: any, props: Array<string>, search: any, strict: boolean = false): any[] {
if (!Array.isArray(arr) || (!isString(search) && !isNumber(search))) {
return arr;
transform(input: any, props: Array<string>, search: any, strict: boolean = false): any[] {
if (!Array.isArray(input) || (!isString(search) && !isNumberFinite(search))) {
return input;
}

search = String(search).toLowerCase();
const term = String(search).toLowerCase();

return arr.filter((obj) => {
return input.filter((obj) => {
return props.some((prop) => {
const value: string = String(
extractDeepPropertyByMapKey(obj, prop)
).toLowerCase();

return strict
? search === value
: !!~value.indexOf(search);
? term === value
: !!~value.indexOf(term);
});
});
}
Expand Down
22 changes: 12 additions & 10 deletions src/app/pipes/array/flatten.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,23 @@ import {PipeTransform, Pipe} from '@angular/core';
@Pipe({name: 'flatten'})
export class FlattenPipe implements PipeTransform {

transform(arr: any, shallow: boolean = false): any[] {
if (!Array.isArray(arr)) {
return arr;
transform(input: any, shallow: boolean = false): any[] {
if (!Array.isArray(input)) {
return input;
}

return shallow
? [].concat.apply([], arr)
: this.flatten(arr);
? [].concat.apply([], input)
: this.flatten(input);
}

private flatten(array: any[]): any[] {
return array.reduce((arr: any[], elm: any) =>
Array.isArray(elm)
? arr.concat(this.flatten(elm))
: arr.concat(elm)
, []);
return array.reduce((arr: any[], elm: any) => {
if (Array.isArray(elm)) {
return arr.concat(this.flatten(elm));
}

return arr.concat(elm);
}, []);
}
}
37 changes: 22 additions & 15 deletions src/app/pipes/array/group-by.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,35 @@ import {extractDeepPropertyByMapKey, isFunction} from '../helpers/helpers';
@Pipe({name: 'groupBy'})
export class GroupByPipe implements PipeTransform {

transform(arr: any, discriminator: any = []): any {
if (!Array.isArray(arr)) {
return arr;
transform(input: any, discriminator: any = []): any {
if (!Array.isArray(input)) {
return input;
}

return this.groupBy(arr, discriminator);
return this.groupBy(input, discriminator);
}

private groupBy(list: any[], discriminator: any) {
return list.reduce((acc, payload) => {
let key;
if (isFunction(discriminator)) {
key = (<Function>discriminator)(payload);
} else if (Array.isArray(discriminator)) {
key = discriminator.map(k => extractDeepPropertyByMapKey(payload, k)).join('_');
} else {
key = extractDeepPropertyByMapKey(payload, <string>discriminator);
}

return acc[key] = Array.isArray(acc[key])
const key = this.extractKeyByDiscriminator(discriminator, payload);

acc[key] = Array.isArray(acc[key])
? acc[key].concat([payload])
: [payload], acc;
: [payload];

return acc;
}, {});
}

private extractKeyByDiscriminator(discriminator, payload) {
if (isFunction(discriminator)) {
return (<Function>discriminator)(payload);
}

if (Array.isArray(discriminator)) {
return discriminator.map(k => extractDeepPropertyByMapKey(payload, k)).join('_');
}

return extractDeepPropertyByMapKey(payload, <string>discriminator);
}
}
8 changes: 4 additions & 4 deletions src/app/pipes/array/initial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import {PipeTransform, Pipe} from '@angular/core';
@Pipe({name: 'initial'})
export class InitialPipe implements PipeTransform {

transform(arr: any, num: number = 0): any[] {
return Array.isArray(arr)
? arr.slice(0, arr.length - num)
: arr;
transform(input: any, num: number = 0): any[] {
return Array.isArray(input)
? input.slice(0, input.length - num)
: input;
}
}
10 changes: 4 additions & 6 deletions src/app/pipes/array/intersection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ import {PipeTransform, Pipe} from '@angular/core';
@Pipe({name: 'intersection'})
export class IntersectionPipe implements PipeTransform {

transform(arr: any, ...args: any[]): any[] {
if (!Array.isArray(arr)) {
return arr;
transform(input: any, ...args: any[]): any[] {
if (!Array.isArray(input)) {
return input;
}

return args.reduce((newArr, currArr) => {
return newArr.filter(elm => !!~currArr.indexOf(elm));
}, arr);
return args.reduce((n, c) => n.filter(e => !!~c.indexOf(e)), input);
}
}
8 changes: 4 additions & 4 deletions src/app/pipes/array/order-by.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import {isString, extractDeepPropertyByMapKey} from '../helpers/helpers';
@Pipe({name: 'orderBy'})
export class OrderByPipe implements PipeTransform {

transform(arr: any, config?: any): any[] {
if (!Array.isArray(arr)) {
return arr;
transform(input: any, config?: any): any[] {
if (!Array.isArray(input)) {
return input;
}

const out = [...arr];
const out = [...input];

// sort by multiple properties
if (Array.isArray(config)) {
Expand Down
8 changes: 4 additions & 4 deletions src/app/pipes/array/pluck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import {extractDeepPropertyByMapKey} from '../helpers/helpers';
@Pipe({name: 'pluck'})
export class PluckPipe implements PipeTransform {

transform(arr: any, map: string): any[] {
return Array.isArray(arr)
? arr.map(e => extractDeepPropertyByMapKey(e, map))
: arr;
transform(input: any, map: string): any[] {
return Array.isArray(input)
? input.map(e => extractDeepPropertyByMapKey(e, map))
: input;
}
}
12 changes: 6 additions & 6 deletions src/app/pipes/array/reverse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import {isString} from '../helpers/helpers';
@Pipe({name: 'reverse'})
export class ReversePipe implements PipeTransform {

transform(value: any): any
transform(input: any): any
{
if (isString(value)) {
return value.split('').reverse().join('');
if (isString(input)) {
return input.split('').reverse().join('');
}

return Array.isArray(value)
? value.reverse()
: value;
return Array.isArray(input)
? input.reverse()
: input;
}
}
14 changes: 8 additions & 6 deletions src/app/pipes/array/sample.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ import {PipeTransform, Pipe} from '@angular/core';
@Pipe({name: 'sample'})
export class SamplePipe implements PipeTransform {

transform(arr: any, len: number = 1): any[] {
if (!Array.isArray(arr)) {
return arr;
transform(input: any, len: number = 1): any[] {
if (!Array.isArray(input)) {
return input;
}

let sample = [];
for (let i = 0, tmp = [...arr], l = len < tmp.length ? len : tmp.length; i < l; ++i) {
const j = Math.floor(Math.random() * tmp.length);
sample = sample.concat(tmp.splice(j ,1));
for (let i = 0, tmp = [...input], l = len < tmp.length ? len : tmp.length; i < l; ++i) {
sample = sample.concat(tmp.splice(
Math.floor(Math.random() * tmp.length),
1
));
}

return sample;
Expand Down
10 changes: 5 additions & 5 deletions src/app/pipes/array/shuffle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ export class ShufflePipe implements PipeTransform {

// Using a version of the Fisher-Yates shuffle algorithm
// https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
transform(arr: any): any[] {
if (!Array.isArray(arr)) {
return arr;
transform(input: any): any[] {
if (!Array.isArray(input)) {
return input;
}

let shuffled = [...arr];
for (let i = 0, n = arr.length - 1, l = n - 1; i < l; ++i) {
let shuffled = [...input];
for (let i = 0, n = input.length - 1, l = n - 1; i < l; ++i) {
const j = Math.floor(Math.random() * (n - i + 1)) + i;
[shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]];
}
Expand Down
4 changes: 2 additions & 2 deletions src/app/pipes/array/some.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {PipeTransform, Pipe} from '@angular/core';
@Pipe({name: 'some'})
export class SomePipe implements PipeTransform {

transform(arr: any, predicate: (value: any, index: number, array: any[]) => boolean): boolean {
return Array.isArray(arr) ? arr.some(predicate) : arr;
transform(input: any, predicate: (value: any, index: number, array: any[]) => boolean): boolean {
return Array.isArray(input) ? input.some(predicate) : input;
}
}
4 changes: 2 additions & 2 deletions src/app/pipes/array/tail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {PipeTransform, Pipe} from '@angular/core';
@Pipe({name: 'tail'})
export class TailPipe implements PipeTransform {

transform(arr: any, num: number = 0): any[] {
return Array.isArray(arr) ? arr.slice(num) : arr;
transform(input: any, num: number = 0): any[] {
return Array.isArray(input) ? input.slice(num) : input;
}
}
8 changes: 4 additions & 4 deletions src/app/pipes/array/truthify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import {PipeTransform, Pipe} from '@angular/core';
@Pipe({name: 'truthify'})
export class TrurthifyPipe implements PipeTransform {

transform(arr: any): any[] {
return Array.isArray(arr)
? arr.filter(elm => !!elm)
: arr;
transform(input: any): any[] {
return Array.isArray(input)
? input.filter(e => !!e)
: input;
}
}
8 changes: 4 additions & 4 deletions src/app/pipes/array/union.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import {PipeTransform, Pipe} from '@angular/core';
@Pipe({name: 'union'})
export class UnionPipe implements PipeTransform {

transform(arr: any, args: any[] = []): any[] {
if (!Array.isArray(arr) || !Array.isArray(args)) {
return arr;
transform(input: any, args: any[] = []): any[] {
if (!Array.isArray(input) || !Array.isArray(args)) {
return input;
}

return args.reduce((newArr, currArr) => {
Expand All @@ -14,6 +14,6 @@ export class UnionPipe implements PipeTransform {
? noDupArr.concat([curr])
: noDupArr;
}, []));
}, arr);
}, input);
}
}
8 changes: 4 additions & 4 deletions src/app/pipes/array/unique.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import {PipeTransform, Pipe} from '@angular/core';
@Pipe({name: 'unique'})
export class UniquePipe implements PipeTransform {

transform(arr: any): any[] {
return Array.isArray(arr)
? arr.filter((elm, index) => arr.indexOf(elm) === index)
: arr;
transform(input: any): any[] {
return Array.isArray(input)
? input.filter((e, i) => input.indexOf(e) === i)
: input;
}
}
8 changes: 4 additions & 4 deletions src/app/pipes/array/without.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import {PipeTransform, Pipe} from '@angular/core';
@Pipe({name: 'without'})
export class WithoutPipe implements PipeTransform {

transform(arr: any, args: any[] = []): any[] {
return Array.isArray(arr)
? arr.filter(elm => !~args.indexOf(elm))
: arr;
transform(input: any, args: any[] = []): any[] {
return Array.isArray(input)
? input.filter(e => !~args.indexOf(e))
: input;
}
}
4 changes: 2 additions & 2 deletions src/app/pipes/boolean/is-array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {PipeTransform, Pipe} from '@angular/core';
@Pipe({name: 'isArray'})
export class IsArrayPipe implements PipeTransform {

transform(value: any): boolean {
return Array.isArray(value);
transform(input: any): boolean {
return Array.isArray(input);
}
}
4 changes: 2 additions & 2 deletions src/app/pipes/boolean/is-defined.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {isUndefined} from '../helpers/helpers';
@Pipe({name: 'isDefined'})
export class IsDefinedPipe implements PipeTransform {

transform(value: any): boolean {
return !isUndefined(value);
transform(input: any): boolean {
return !isUndefined(input);
}
}
4 changes: 2 additions & 2 deletions src/app/pipes/boolean/is-equal-to.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {PipeTransform, Pipe} from '@angular/core';
@Pipe({name: 'isEqualTo'})
export class IsEqualToPipe implements PipeTransform {

transform(value: any, other: any): boolean {
return value == other;
transform(input: any, other: any): boolean {
return input == other;
}
}
Loading

0 comments on commit 4c4329f

Please sign in to comment.