diff --git a/README.md b/README.md index a93b3811..d4cd8cf1 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,8 @@ - [latinise](#latinise) - [lines](#lines) - [underscore](#underscore) + - [test](#test) + - [match](#match) - [Array](#Array) - [diff](#diff) - [flatten](#flatten) @@ -278,6 +280,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/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..eceb523a 100644 --- a/src/app/pipes/string/index.ts +++ b/src/app/pipes/string/index.ts @@ -13,17 +13,20 @@ 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({ - declarations: [...STRING_PIPES], + declarations: STRING_PIPES, imports: [], - exports: [...STRING_PIPES] + exports: STRING_PIPES }) export class NgStringPipesModule {} @@ -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); + } +}