Skip to content

Commit

Permalink
Merge branch 'next'
Browse files Browse the repository at this point in the history
  • Loading branch information
danrevah committed Dec 13, 2016
2 parents 2237c94 + 8eb688c commit 2d366df
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 9 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
- [latinise](#latinise)
- [lines](#lines)
- [underscore](#underscore)
- [test](#test)
- [match](#match)
- [Array](#Array)
- [diff](#diff)
- [flatten](#flatten)
Expand Down Expand Up @@ -278,6 +280,30 @@ API: `string | underscore`
<p>{{'FooBar' | underscore }}</p> <!-- Output: "foo_bar" -->
```

### test

Tests if a string matches a pattern.

API: `string | test: {RegExp}: {Flags}`

```html
<p>{{'foo 42' | test: '[\\d]+$': 'g' }}</p> <!-- Output: true -->
<p>{{'42 foo' | test: '[\\d]+$': 'g' }}</p> <!-- Output: false -->
<p>{{'FOO' | test: '^foo': 'i' }}</p> <!-- Output: true -->
```

### match

Returns array of matched elements in string.

API: `string | match: {RegExp}: {Flags}`

```html
<p>{{'foo 42' | match: '[\\d]+$': 'g' }}</p> <!-- Output: '42' -->
<p>{{'42 foo' | match: '[\\d]+$': 'g' }}</p> <!-- Output: null -->
<p>{{'FOO' | match: '^foo': 'i' }}</p> <!-- Output: 'FOO' -->
```

## Array

### diff
Expand Down
4 changes: 2 additions & 2 deletions src/app/pipes/array/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ const ARRAY_PIPES = [
];

@NgModule({
declarations: [ ...ARRAY_PIPES ],
declarations: ARRAY_PIPES,
imports: [],
exports: [ ...ARRAY_PIPES ]
exports: ARRAY_PIPES
})
export class NgArrayPipesModule {}

Expand Down
4 changes: 2 additions & 2 deletions src/app/pipes/boolean/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}

Expand Down
4 changes: 2 additions & 2 deletions src/app/pipes/math/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}

Expand Down
11 changes: 8 additions & 3 deletions src/app/pipes/string/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}

Expand All @@ -41,3 +44,5 @@ export * from './camelize';
export * from './latinise';
export * from './lines';
export * from './underscore';
export * from './match';
export * from './test';
24 changes: 24 additions & 0 deletions src/app/pipes/string/match.spec.ts
Original file line number Diff line number Diff line change
@@ -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']);
});
});
14 changes: 14 additions & 0 deletions src/app/pipes/string/match.ts
Original file line number Diff line number Diff line change
@@ -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));
}
}
24 changes: 24 additions & 0 deletions src/app/pipes/string/test.spec.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
14 changes: 14 additions & 0 deletions src/app/pipes/string/test.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 2d366df

Please sign in to comment.