-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Invert & InvertBy): Added invert and invertBy pipes
- Loading branch information
danrevah
committed
Dec 10, 2016
1 parent
b8ac3c1
commit 4a88c9a
Showing
6 changed files
with
137 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import {InvertByPipe} from './invert-by'; | ||
|
||
describe('InvertBy Pipe', () => { | ||
let pipe: InvertByPipe; | ||
|
||
beforeEach(() => { | ||
pipe = new InvertByPipe(); | ||
}); | ||
|
||
it('should keep the element the same way if its not an object', () => { | ||
expect(pipe.transform([1, 2, 3])).toEqual([1, 2, 3]); | ||
expect(pipe.transform([])).toEqual([]); | ||
expect(pipe.transform('foo')).toEqual('foo'); | ||
expect(pipe.transform(null)).toEqual(null); | ||
expect(pipe.transform(undefined)).toEqual(undefined); | ||
}); | ||
|
||
it('should return inverted values / keys object', () => { | ||
expect(pipe.transform({})).toEqual({}); | ||
expect(pipe.transform({foo: 'bar'})).toEqual({bar: ['foo']}); | ||
expect(pipe.transform({foo: 1, bar: 42})).toEqual({1: ['foo'], 42: ['bar']}); | ||
|
||
expect(pipe.transform({}, _ => `name_${_}`)).toEqual({}); | ||
expect(pipe.transform({foo: 'bar'}, _ => `name_${_}`)).toEqual({name_bar: ['foo']}); | ||
expect(pipe.transform({foo: 1, bar: 42}, _ => `name_${_}`)).toEqual({name_1: ['foo'], name_42: ['bar']}); | ||
}); | ||
|
||
it('should return inverted values / keys subsequent values override previous value', () => { | ||
expect(pipe.transform({a: 1, b: 2, c: 1})).toEqual({1: ['a', 'c'], 2: ['b']}); | ||
expect(pipe.transform({a: 1, b: 2, c: 1, d: 2, e: 3})).toEqual({1: ['a','c'], 2: ['b','d'], 3: ['e']}); | ||
|
||
expect(pipe.transform({a: 1, b: 2, c: 1}, _ => `name_${_}`)).toEqual({name_1: ['a', 'c'], name_2: ['b']}); | ||
expect(pipe.transform({a: 1, b: 2, c: 1, d: 2, e: 3}, _ => `name_${_}`)).toEqual({name_1: ['a','c'], name_2: ['b','d'], name_3: ['e']}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import {PipeTransform, Pipe} from '@angular/core'; | ||
import GeneralHelper from '../helpers/helpers'; | ||
|
||
@Pipe({name: 'invertBy'}) | ||
export class InvertByPipe implements PipeTransform { | ||
|
||
transform(obj: any, cb: Function = null): Object { | ||
if (Array.isArray(obj) || !GeneralHelper.isObject(obj)) { | ||
return obj; | ||
} | ||
|
||
return Object.keys(obj) | ||
.reduce((o, k) => { | ||
const key = cb ? cb(obj[k]) : obj[k]; | ||
return Array.isArray(o[key]) | ||
? (o[key].push(k), o) | ||
: Object.assign(o, {[key]: [k]}); | ||
}, {}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import {InvertPipe} from './invert'; | ||
|
||
describe('Invert Pipe', () => { | ||
let pipe: InvertPipe; | ||
|
||
beforeEach(() => { | ||
pipe = new InvertPipe(); | ||
}); | ||
|
||
it('should keep the element the same way if its not an object', () => { | ||
expect(pipe.transform([1, 2, 3])).toEqual([1, 2, 3]); | ||
expect(pipe.transform([])).toEqual([]); | ||
expect(pipe.transform('foo')).toEqual('foo'); | ||
expect(pipe.transform(null)).toEqual(null); | ||
expect(pipe.transform(undefined)).toEqual(undefined); | ||
}); | ||
|
||
it('should return inverted values / keys object', () => { | ||
expect(pipe.transform({})).toEqual({}); | ||
expect(pipe.transform({foo: 'bar'})).toEqual({bar: 'foo'}); | ||
expect(pipe.transform({foo: 1, bar: 42})).toEqual({1: 'foo', 42: 'bar'}); | ||
expect(pipe.transform({foo: [1, 2], bar: 42})).toEqual({'1,2': 'foo', 42: 'bar'}); | ||
expect(pipe.transform({foo: {a: 1, b: 2}, bar: 42})).toEqual({'[object Object]': 'foo', 42: 'bar'}); | ||
}); | ||
|
||
it('should return inverted values / keys subsequent values override previous value', () => { | ||
expect(pipe.transform({a: 1, b: 2, c: 1})).toEqual({1: 'c', 2: 'b'}); | ||
expect(pipe.transform({a: 1, b: 2, c: 1, d: 2, e: 3})).toEqual({1: 'c', 2: 'd', 3: 'e'}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import {PipeTransform, Pipe} from '@angular/core'; | ||
import GeneralHelper from '../helpers/helpers'; | ||
|
||
@Pipe({name: 'invert'}) | ||
export class InvertPipe implements PipeTransform { | ||
|
||
transform(obj: any): Object { | ||
if (Array.isArray(obj) || !GeneralHelper.isObject(obj)) { | ||
return obj; | ||
} | ||
|
||
return Object.keys(obj) | ||
.reduce((o, k) => Object.assign(o, {[obj[k]]: k}), {}); | ||
} | ||
} |