From fda76494ef6ac418646ec597d3550498dacb3773 Mon Sep 17 00:00:00 2001 From: danrevah Date: Sun, 11 Dec 2016 20:16:54 +0200 Subject: [PATCH 1/2] Adjust pipes exports --- src/app/pipes/array/index.ts | 4 ++-- src/app/pipes/boolean/index.ts | 4 ++-- src/app/pipes/math/index.ts | 4 ++-- src/app/pipes/string/index.ts | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/app/pipes/array/index.ts b/src/app/pipes/array/index.ts index af74510c..89291a9c 100644 --- a/src/app/pipes/array/index.ts +++ b/src/app/pipes/array/index.ts @@ -23,9 +23,9 @@ const ARRAY_PIPES = [ ]; @NgModule({ - declarations: [ ...ARRAY_PIPES ], + declarations: ARRAY_PIPES, imports: [], - exports: [ ...ARRAY_PIPES ] + exports: ARRAY_PIPES }) export class NgArrayPipesModule {} diff --git a/src/app/pipes/boolean/index.ts b/src/app/pipes/boolean/index.ts index 0f002f44..47781e6e 100644 --- a/src/app/pipes/boolean/index.ts +++ b/src/app/pipes/boolean/index.ts @@ -24,9 +24,9 @@ export const BOOLEAN_PIPES = [ ]; @NgModule({ - declarations: [...BOOLEAN_PIPES], + declarations: BOOLEAN_PIPES, imports: [], - exports: [...BOOLEAN_PIPES] + exports: BOOLEAN_PIPES }) export class NgBooleanPipesModule {} diff --git a/src/app/pipes/math/index.ts b/src/app/pipes/math/index.ts index c507915f..708e892f 100644 --- a/src/app/pipes/math/index.ts +++ b/src/app/pipes/math/index.ts @@ -18,9 +18,9 @@ export const MATH_PIPES = [ ]; @NgModule({ - declarations: [...MATH_PIPES], + declarations: MATH_PIPES, imports: [], - exports: [...MATH_PIPES] + exports: MATH_PIPES }) export class NgMathPipesModule {} diff --git a/src/app/pipes/string/index.ts b/src/app/pipes/string/index.ts index ca30dc53..de3a6cc2 100644 --- a/src/app/pipes/string/index.ts +++ b/src/app/pipes/string/index.ts @@ -21,9 +21,9 @@ export const STRING_PIPES = [ ]; @NgModule({ - declarations: [...STRING_PIPES], + declarations: STRING_PIPES, imports: [], - exports: [...STRING_PIPES] + exports: STRING_PIPES }) export class NgStringPipesModule {} From 8eb688c6665d180e92685642c45e487c7def1801 Mon Sep 17 00:00:00 2001 From: danrevah Date: Tue, 13 Dec 2016 13:02:50 +0200 Subject: [PATCH 2/2] feat(String-Pipes): Add test & match pipes --- README.md | 26 ++++++++++++++++++++++++++ src/app/pipes/string/index.ts | 7 ++++++- src/app/pipes/string/match.spec.ts | 24 ++++++++++++++++++++++++ src/app/pipes/string/match.ts | 14 ++++++++++++++ src/app/pipes/string/test.spec.ts | 24 ++++++++++++++++++++++++ src/app/pipes/string/test.ts | 14 ++++++++++++++ 6 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 src/app/pipes/string/match.spec.ts create mode 100644 src/app/pipes/string/match.ts create mode 100644 src/app/pipes/string/test.spec.ts create mode 100644 src/app/pipes/string/test.ts diff --git a/README.md b/README.md index c4516f1a..febaf191 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,8 @@ - [latinise](#latinise) - [lines](#lines) - [underscore](#underscore) + - [test](#test) + - [match](#match) - [Array](#Array) - [diff](#diff) - [flatten](#flatten) @@ -283,6 +285,30 @@ API: `string | underscore`

{{'FooBar' | underscore }}

``` +### test + +Tests if a string matches a pattern. + +API: `string | test: {RegExp}: {Flags}` + +```html +

{{'foo 42' | test: '[\\d]+$': 'g' }}

+

{{'42 foo' | test: '[\\d]+$': 'g' }}

+

{{'FOO' | test: '^foo': 'i' }}

+``` + +### match + +Returns array of matched elements in string. + +API: `string | match: {RegExp}: {Flags}` + +```html +

{{'foo 42' | match: '[\\d]+$': 'g' }}

+

{{'42 foo' | match: '[\\d]+$': 'g' }}

+

{{'FOO' | match: '^foo': 'i' }}

+``` + ## Array ### diff diff --git a/src/app/pipes/string/index.ts b/src/app/pipes/string/index.ts index de3a6cc2..eceb523a 100644 --- a/src/app/pipes/string/index.ts +++ b/src/app/pipes/string/index.ts @@ -13,11 +13,14 @@ import {CamelizePipe} from './camelize'; import {LatinisePipe} from './latinise'; import {LinesPipe} from './lines'; import {UnderscorePipe} from './underscore'; +import {MatchPipe} from './match'; +import {TestPipe} from './test'; export const STRING_PIPES = [ LeftTrimPipe, RepeatPipe, RightTrimPipe, ScanPipe, ShortenPipe, StripTagsPipe, TrimPipe, UcFirstPipe, UcWordsPipe, SlugifyPipe, - CamelizePipe, LatinisePipe, LinesPipe, UnderscorePipe + CamelizePipe, LatinisePipe, LinesPipe, UnderscorePipe, MatchPipe, + TestPipe ]; @NgModule({ @@ -41,3 +44,5 @@ export * from './camelize'; export * from './latinise'; export * from './lines'; export * from './underscore'; +export * from './match'; +export * from './test'; diff --git a/src/app/pipes/string/match.spec.ts b/src/app/pipes/string/match.spec.ts new file mode 100644 index 00000000..52c90c15 --- /dev/null +++ b/src/app/pipes/string/match.spec.ts @@ -0,0 +1,24 @@ +import {MatchPipe} from './match'; + +describe('MatchPipe Tests', () => { + let pipe:MatchPipe; + + beforeEach(() => { + pipe = new MatchPipe(); + }); + + it('Should not do anything if not a string', () => { + expect(pipe.transform(null, '')).toEqual(null); + expect(pipe.transform(undefined, '')).toEqual(undefined); + expect(pipe.transform(42, '')).toEqual(42); + expect(pipe.transform({name: 'foo'}, '')).toEqual({name: 'foo'}); + }); + + it('Should camelize properly', () => { + expect(pipe.transform('foo 42', '[\\d]+$', 'g')).toEqual(['42']); + expect(pipe.transform('42 foo', '[\\d]+$', 'g')).toEqual(null); + expect(pipe.transform('foo', '[\\d]+$', 'g')).toEqual(null); + expect(pipe.transform('FOO', '^foo')).toEqual(null); + expect(pipe.transform('FOO', '^foo', 'i')).toBeTruthy(['FOO']); + }); +}); diff --git a/src/app/pipes/string/match.ts b/src/app/pipes/string/match.ts new file mode 100644 index 00000000..c1c23a8e --- /dev/null +++ b/src/app/pipes/string/match.ts @@ -0,0 +1,14 @@ +import {PipeTransform, Pipe} from '@angular/core'; +import GeneralHelper from '../helpers/helpers'; + +@Pipe({name: 'match'}) +export class MatchPipe implements PipeTransform { + + transform(text: any, pattern: string, flags?: string): boolean { + if (!GeneralHelper.isString(text)) { + return text; + } + + return text.match(new RegExp(pattern, flags)); + } +} diff --git a/src/app/pipes/string/test.spec.ts b/src/app/pipes/string/test.spec.ts new file mode 100644 index 00000000..248471ce --- /dev/null +++ b/src/app/pipes/string/test.spec.ts @@ -0,0 +1,24 @@ +import {TestPipe} from './test'; + +describe('TestPipe Tests', () => { + let pipe:TestPipe; + + beforeEach(() => { + pipe = new TestPipe(); + }); + + it('Should not do anything if not a string', () => { + expect(pipe.transform(null, '')).toEqual(null); + expect(pipe.transform(undefined, '')).toEqual(undefined); + expect(pipe.transform(42, '')).toEqual(42); + expect(pipe.transform({name: 'foo'}, '')).toEqual({name: 'foo'}); + }); + + it('Should camelize properly', () => { + expect(pipe.transform('foo 42', '[\\d]+$', 'g')).toBeTruthy(); + expect(pipe.transform('42 foo', '[\\d]+$', 'g')).toBeFalsy(); + expect(pipe.transform('foo', '[\\d]+$', 'g')).toBeFalsy(); + expect(pipe.transform('FOO', '^foo')).toBeFalsy(); + expect(pipe.transform('FOO', '^foo', 'i')).toBeTruthy(); + }); +}); diff --git a/src/app/pipes/string/test.ts b/src/app/pipes/string/test.ts new file mode 100644 index 00000000..c6a15fc6 --- /dev/null +++ b/src/app/pipes/string/test.ts @@ -0,0 +1,14 @@ +import {PipeTransform, Pipe} from '@angular/core'; +import GeneralHelper from '../helpers/helpers'; + +@Pipe({name: 'test'}) +export class TestPipe implements PipeTransform { + + transform(text: any, pattern: string, flags?: string): boolean { + if (!GeneralHelper.isString(text)) { + return text; + } + + return (new RegExp(pattern, flags)).test(text); + } +}