Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: convert all pipes to standalone #252

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/ng-pipes/pipes/array/chunk.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { Pipe, PipeTransform } from '@angular/core';
import { isString } from '../helpers/helpers';

@Pipe({ name: 'chunk' })
@Pipe({
name: 'chunk',
standalone: true,
})
export class ChunkPipe implements PipeTransform {
transform(input: any, size: number = 1): any {
if (isString(input)) {
return this.chunk(input
.split(''), size);
return this.chunk(input.split(''), size);
}

return Array.isArray(input) ? this.chunk(input, size) : input;
Expand All @@ -16,6 +18,6 @@ export class ChunkPipe implements PipeTransform {
return Array(Math.ceil(input.length / size))
.fill([])
.map((_, index) => index * size)
.map(begin => input.slice(begin, begin + size));
.map((begin) => input.slice(begin, begin + size));
}
}
5 changes: 4 additions & 1 deletion src/ng-pipes/pipes/array/diff.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { Pipe, PipeTransform } from '@angular/core';

@Pipe({ name: 'diff' })
@Pipe({
name: 'diff',
standalone: true,
})
export class DiffPipe implements PipeTransform {
transform<T>(input: T, ...args: any[]): T;
transform(input: any[], ...args: any[]): any[];
Expand Down
5 changes: 4 additions & 1 deletion src/ng-pipes/pipes/array/every.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { Pipe, PipeTransform } from '@angular/core';

@Pipe({ name: 'every' })
@Pipe({
name: 'every',
standalone: true,
})
export class EveryPipe implements PipeTransform {
transform(input: any, predicate: (value: any, index: number, array: any[]) => boolean): boolean | any[] {
return Array.isArray(input) ? input.every(predicate) : false;
Expand Down
6 changes: 5 additions & 1 deletion src/ng-pipes/pipes/array/filter-by-impure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,9 @@ import { Pipe } from '@angular/core';
import { FilterByPipe } from './filter-by';

// tslint:disable use-pipe-transform-interface
@Pipe({ name: 'filterByImpure', pure: false })
@Pipe({
name: 'filterByImpure',
pure: false,
standalone: true,
})
export class FilterByImpurePipe extends FilterByPipe {}
17 changes: 9 additions & 8 deletions src/ng-pipes/pipes/array/filter-by.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import {
} from '../helpers/helpers';

// tslint:disable no-bitwise
@Pipe({ name: 'filterBy' })
@Pipe({
name: 'filterBy',
standalone: true,
})
export class FilterByPipe implements PipeTransform {
transform<T>(input: T, props: Array<string>, search?: any, strict?: boolean): T;
transform(input: any[], props: Array<string>, search?: any, strict?: boolean): any[];
Expand All @@ -21,19 +24,17 @@ export class FilterByPipe implements PipeTransform {
return input;
}

const terms = String(search)
.toLowerCase()
.split(',');
const terms = String(search).toLowerCase().split(',');

return input.filter(obj => {
return props.some(prop => {
return terms.some(term => {
return input.filter((obj) => {
return props.some((prop) => {
return terms.some((term) => {
const value = extractDeepPropertyByMapKey(obj, prop);
/* tslint:disable */
const { props, tail } = extractDeepPropertyByParentMapKey(obj, prop);

if (isUndefined(value) && !isUndefined(props) && Array.isArray(props)) {
return props.some(parent => {
return props.some((parent) => {
const str = String(parent[tail]).toLowerCase();

return strict ? str === term : !!~str.indexOf(term);
Expand Down
5 changes: 4 additions & 1 deletion src/ng-pipes/pipes/array/flatten.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { Pipe, PipeTransform } from '@angular/core';

@Pipe({ name: 'flatten' })
@Pipe({
name: 'flatten',
standalone: true,
})
export class FlattenPipe implements PipeTransform {
transform<T>(input: T, shallow?: boolean): T;
transform(input: any[], shallow?: boolean): any[];
Expand Down
7 changes: 6 additions & 1 deletion src/ng-pipes/pipes/array/from-pairs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ describe('FromPairsPipe', () => {

it('should transform array to object', () => {
expect(pipe.transform([['foo', 42]])).toEqual({ foo: 42 });
expect(pipe.transform([['foo', 42], ['bar', 'foobar']])).toEqual({ foo: 42, bar: 'foobar' });
expect(
pipe.transform([
['foo', 42],
['bar', 'foobar'],
])
).toEqual({ foo: 42, bar: 'foobar' });
});

it('should ignore non-array elements', () => {
Expand Down
5 changes: 4 additions & 1 deletion src/ng-pipes/pipes/array/from-pairs.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { Pipe, PipeTransform } from '@angular/core';

@Pipe({ name: 'fromPairs' })
@Pipe({
name: 'fromPairs',
standalone: true,
})
export class FromPairsPipe implements PipeTransform {
transform(input: any): any {
if (!Array.isArray(input)) {
Expand Down
6 changes: 5 additions & 1 deletion src/ng-pipes/pipes/array/group-by-impure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,9 @@ import { Pipe } from '@angular/core';
import { GroupByPipe } from './group-by';

// tslint:disable use-pipe-transform-interface
@Pipe({ name: 'groupByImpure', pure: false })
@Pipe({
name: 'groupByImpure',
pure: false,
standalone: true,
})
export class GroupByImpurePipe extends GroupByPipe {}
40 changes: 32 additions & 8 deletions src/ng-pipes/pipes/array/group-by.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,14 @@ describe('GroupByPipe', () => {
];
const result = pipe.transform(arrayWithDiscriminator, ['key', 'type'], '_');
expect(result).toEqual({
foo_1: [{ id: 1, key: 'foo', type: 1 }, { id: 3, key: 'foo', type: 1 }],
foo_2: [{ id: 2, key: 'foo', type: 2 }, { id: 4, key: 'foo', type: 2 }],
foo_1: [
{ id: 1, key: 'foo', type: 1 },
{ id: 3, key: 'foo', type: 1 },
],
foo_2: [
{ id: 2, key: 'foo', type: 2 },
{ id: 4, key: 'foo', type: 2 },
],
});
});

Expand All @@ -60,8 +66,14 @@ describe('GroupByPipe', () => {
];
const result = pipe.transform(arrayWithDiscriminator, ['key', 'type']);
expect(result).toEqual({
'foo|1': [{ id: 1, key: 'foo', type: 1 }, { id: 3, key: 'foo', type: 1 }],
'foo|2': [{ id: 2, key: 'foo', type: 2 }, { id: 4, key: 'foo', type: 2 }],
'foo|1': [
{ id: 1, key: 'foo', type: 1 },
{ id: 3, key: 'foo', type: 1 },
],
'foo|2': [
{ id: 2, key: 'foo', type: 2 },
{ id: 4, key: 'foo', type: 2 },
],
});
});

Expand All @@ -74,8 +86,14 @@ describe('GroupByPipe', () => {
];
const result = pipe.transform(arrayWithDiscriminator, 'prop.deep');
expect(result).toEqual({
foo: [{ id: 1, prop: { deep: 'foo' } }, { id: 3, prop: { deep: 'foo' } }],
bar: [{ id: 2, prop: { deep: 'bar' } }, { id: 4, prop: { deep: 'bar' } }],
foo: [
{ id: 1, prop: { deep: 'foo' } },
{ id: 3, prop: { deep: 'foo' } },
],
bar: [
{ id: 2, prop: { deep: 'bar' } },
{ id: 4, prop: { deep: 'bar' } },
],
});
});

Expand All @@ -88,8 +106,14 @@ describe('GroupByPipe', () => {
];
const result = pipe.transform(arrayWithDiscriminator, ['prop.deep', 'prop.type']);
expect(result).toEqual({
'foo|1': [{ id: 1, prop: { deep: 'foo', type: 1 } }, { id: 3, prop: { deep: 'foo', type: 1 } }],
'foo|2': [{ id: 2, prop: { deep: 'foo', type: 2 } }, { id: 4, prop: { deep: 'foo', type: 2 } }],
'foo|1': [
{ id: 1, prop: { deep: 'foo', type: 1 } },
{ id: 3, prop: { deep: 'foo', type: 1 } },
],
'foo|2': [
{ id: 2, prop: { deep: 'foo', type: 2 } },
{ id: 4, prop: { deep: 'foo', type: 2 } },
],
});
});
});
7 changes: 5 additions & 2 deletions src/ng-pipes/pipes/array/group-by.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { Pipe, PipeTransform } from '@angular/core';
import { extractDeepPropertyByMapKey, isFunction } from '../helpers/helpers';

@Pipe({ name: 'groupBy' })
@Pipe({
name: 'groupBy',
standalone: true,
})
export class GroupByPipe implements PipeTransform {
transform(input: any, discriminator: any = [], delimiter: string = '|'): any {
if (!Array.isArray(input)) {
Expand All @@ -27,7 +30,7 @@ export class GroupByPipe implements PipeTransform {
}

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

return extractDeepPropertyByMapKey(payload, <string>discriminator);
Expand Down
5 changes: 2 additions & 3 deletions src/ng-pipes/pipes/array/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,11 @@ const ARRAY_PIPES = [
OrderByImpurePipe,
RangePipe,
ChunkPipe,
FromPairsPipe
FromPairsPipe,
];

@NgModule({
declarations: ARRAY_PIPES,
imports: [],
imports: [...ARRAY_PIPES],
exports: ARRAY_PIPES,
})
export class NgArrayPipesModule {}
Expand Down
5 changes: 4 additions & 1 deletion src/ng-pipes/pipes/array/initial.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { Pipe, PipeTransform } from '@angular/core';

@Pipe({ name: 'initial' })
@Pipe({
name: 'initial',
standalone: true,
})
export class InitialPipe implements PipeTransform {
transform(input: any[], num: number): any[];
transform(input: any): any;
Expand Down
5 changes: 4 additions & 1 deletion src/ng-pipes/pipes/array/intersection.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { Pipe, PipeTransform } from '@angular/core';

@Pipe({ name: 'intersection' })
@Pipe({
name: 'intersection',
standalone: true,
})
export class IntersectionPipe implements PipeTransform {
transform<T>(input: T, ...args: any[]): T;
transform(input: any[], ...args: any[]): any[];
Expand Down
6 changes: 5 additions & 1 deletion src/ng-pipes/pipes/array/order-by-impure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,9 @@ import { Pipe } from '@angular/core';
import { OrderByPipe } from './order-by';

// tslint:disable use-pipe-transform-interface
@Pipe({ name: 'orderByImpure', pure: false })
@Pipe({
name: 'orderByImpure',
pure: false,
standalone: true,
})
export class OrderByImpurePipe extends OrderByPipe {}
14 changes: 12 additions & 2 deletions src/ng-pipes/pipes/array/order-by.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,20 @@ describe('OrderByPipe', () => {
it('should order by a property of type boolean', () => {
expect(
pipe.transform(
[{ id: 1, value: false }, { id: 2, value: true }, { id: 3, value: false }, { id: 4, value: true }],
[
{ id: 1, value: false },
{ id: 2, value: true },
{ id: 3, value: false },
{ id: 4, value: true },
],
'value'
)
).toEqual([{ id: 1, value: false }, { id: 3, value: false }, { id: 2, value: true }, { id: 4, value: true }]);
).toEqual([
{ id: 1, value: false },
{ id: 3, value: false },
{ id: 2, value: true },
{ id: 4, value: true },
]);
});

it('should order by a property of type date', () => {
Expand Down
5 changes: 4 additions & 1 deletion src/ng-pipes/pipes/array/order-by.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { Pipe, PipeTransform } from '@angular/core';
import { extractDeepPropertyByMapKey, isString, isUndefined } from '../helpers/helpers';

@Pipe({ name: 'orderBy' })
@Pipe({
name: 'orderBy',
standalone: true,
})
export class OrderByPipe implements PipeTransform {
transform<T>(input: T, config?: any): T;
transform(input: any[], config?: any): any[];
Expand Down
8 changes: 6 additions & 2 deletions src/ng-pipes/pipes/array/pluck.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import { Pipe, PipeTransform } from '@angular/core';
import { extractDeepPropertyByMapKey, isObject } from '../helpers/helpers';

@Pipe({ name: 'pluck', pure: false })
@Pipe({
name: 'pluck',
pure: false,
standalone: true,
})
export class PluckPipe implements PipeTransform {
transform<T, K extends keyof T>(input: T, map: keyof T): T[K];
transform(input: any[], map: string): any[];
transform(input: any, map: string): any;

transform(input: any, map: string): any {
if (Array.isArray(input)) {
return input.map(e => extractDeepPropertyByMapKey(e, map));
return input.map((e) => extractDeepPropertyByMapKey(e, map));
}

return isObject(input) ? extractDeepPropertyByMapKey(input, map) : input;
Expand Down
5 changes: 4 additions & 1 deletion src/ng-pipes/pipes/array/range.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { Pipe, PipeTransform } from '@angular/core';

@Pipe({ name: 'range' })
@Pipe({
name: 'range',
standalone: true,
})
export class RangePipe implements PipeTransform {
transform(start: number = 1, count: number = 0, step: number = 1): any {
return Array(count)
Expand Down
10 changes: 5 additions & 5 deletions src/ng-pipes/pipes/array/reverse.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Pipe, PipeTransform } from '@angular/core';
import { isString } from '../helpers/helpers';

@Pipe({ name: 'reverse' })
@Pipe({
name: 'reverse',
standalone: true,
})
export class ReversePipe implements PipeTransform {
transform(input: any): any {
if (isString(input)) {
return input
.split('')
.reverse()
.join('');
return input.split('').reverse().join('');
}

return Array.isArray(input) ? input.slice().reverse() : input;
Expand Down
5 changes: 4 additions & 1 deletion src/ng-pipes/pipes/array/sample.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { Pipe, PipeTransform } from '@angular/core';

@Pipe({ name: 'sample' })
@Pipe({
name: 'sample',
standalone: true,
})
export class SamplePipe implements PipeTransform {
transform(input: any[], len?: number): any[];
transform<T>(input: T, len?: number): T;
Expand Down
5 changes: 4 additions & 1 deletion src/ng-pipes/pipes/array/shuffle.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { Pipe, PipeTransform } from '@angular/core';

@Pipe({ name: 'shuffle' })
@Pipe({
name: 'shuffle',
standalone: true,
})
export class ShufflePipe implements PipeTransform {
transform<T>(input: T): T;
transform(input: any[]): any[];
Expand Down
Loading