Skip to content

Commit aa342d7

Browse files
committed
Merge pull request #28 from srijken/stringify-exclusions
Fix exclusion patterns starting and ending with *
2 parents 78757e4 + e1de8b1 commit aa342d7

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

src/Utils-spec.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ describe('Utils', () => {
2323
expect(Utils.stringify([{one: afoo, two: afoo}])).toBe('[{"one":{"a":"foo"}}]');
2424
});
2525

26-
it('should exclude properties', () => {
26+
describe('stringify', () => {
2727
var user:any = {
2828
id:1,
2929
name: 'Blake',
@@ -37,11 +37,27 @@ describe('Utils', () => {
3737
}
3838
};
3939

40-
expect(Utils.stringify(user)).toBe(JSON.stringify(user));
41-
expect(Utils.stringify(user, ['pAssword'])).toBe('{"id":1,"name":"Blake","passwordResetToken":"a reset token","myPasswordValue":"123456","myPassword":"123456","customValue":"Password","value":{}}');
42-
expect(Utils.stringify(user, ['*password'])).toBe('{"id":1,"name":"Blake","myPasswordValue":"123456","myPassword":"123456","customValue":"Password","value":{}}');
43-
expect(Utils.stringify(user, ['password*'])).toBe('{"id":1,"name":"Blake","passwordResetToken":"a reset token","myPasswordValue":"123456","customValue":"Password","value":{}}');
44-
expect(Utils.stringify(user, ['*password*'])).toBe('{"id":1,"name":"Blake","customValue":"Password","value":{}}');
40+
it('should behave like JSON.stringify', () => {
41+
expect(Utils.stringify(user)).toBe(JSON.stringify(user));
42+
});
43+
44+
describe('with exclude pattern', () => {
45+
it('pAssword', () => {
46+
expect(Utils.stringify(user, ['pAssword'])).toBe('{"id":1,"name":"Blake","passwordResetToken":"a reset token","myPasswordValue":"123456","myPassword":"123456","customValue":"Password","value":{}}');
47+
});
48+
49+
it('*password', () => {
50+
expect(Utils.stringify(user, ['*password'])).toBe('{"id":1,"name":"Blake","passwordResetToken":"a reset token","myPasswordValue":"123456","customValue":"Password","value":{}}');
51+
});
52+
53+
it('password*', () => {
54+
expect(Utils.stringify(user, ['password*'])).toBe('{"id":1,"name":"Blake","myPasswordValue":"123456","myPassword":"123456","customValue":"Password","value":{}}');
55+
});
56+
57+
it('*password*', () => {
58+
expect(Utils.stringify(user, ['*password*'])).toBe('{"id":1,"name":"Blake","customValue":"Password","value":{}}');
59+
});
60+
});
4561
});
4662

4763
it('should stringify array', () => {

src/Utils.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,15 +133,14 @@ export class Utils {
133133
pattern = pattern.substring(0, pattern.length - 1);
134134
}
135135

136-
137136
if (startsWithWildcard && endsWithWildcard)
138137
return value.indexOf(pattern) !== -1;
139138

140139
if (startsWithWildcard)
141-
return value.lastIndexOf(pattern, 0) !== -1;
140+
return value.lastIndexOf(pattern) === (value.length - pattern.length);
142141

143142
if (endsWithWildcard)
144-
return value.lastIndexOf(pattern) === (value.length - pattern.length);
143+
return value.indexOf(pattern) === 0;
145144

146145
return value === pattern;
147146
}

0 commit comments

Comments
 (0)