This repository has been archived by the owner on Feb 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a pipe that transforms objects to an array of key-value pairs.
- Loading branch information
Showing
6 changed files
with
85 additions
and
2 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
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,25 @@ | ||
import { ToKeyValuePairsPipe } from './to-key-value-pairs'; | ||
|
||
describe('ToKeyValuePairsPipe', () => { | ||
let pipe: ToKeyValuePairsPipe; | ||
|
||
beforeEach(() => { | ||
pipe = new ToKeyValuePairsPipe(); | ||
}); | ||
|
||
const value = { | ||
a: 1, | ||
b: 2, | ||
c: 3, | ||
}; | ||
|
||
it('should transform the object to an array of KVPs', () => { | ||
expect(pipe.transform(value)).toEqual([ | ||
{ key: 'a', value: 1 }, { key: 'b', value: 2 }, { key: 'c', value: 3 }, | ||
]); | ||
}); | ||
|
||
it('should return non-object input unchanged', () => { | ||
expect(pipe.transform('input')).toEqual('input'); | ||
}); | ||
}); |
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,28 @@ | ||
import { NgModule, Pipe, PipeTransform } from '@angular/core'; | ||
import { isObject } from '../utils/utils'; | ||
|
||
/** | ||
* @description Transforms an object to an array of key/value pairs, | ||
* returns the input unchanged when not of type object. | ||
*/ | ||
@Pipe({ name: 'toKeyValuePairs' }) | ||
export class ToKeyValuePairsPipe implements PipeTransform { | ||
transform(input: any): any { | ||
|
||
// Any input not of type object is returned un-mutated. | ||
if (!isObject(input)) { | ||
return input; | ||
} | ||
|
||
// Get the array of key/values of the object and transform into an array of KVPs | ||
return Object.entries(input).map(element => ({ key: element[0], value: element[1] })); | ||
} | ||
|
||
} | ||
|
||
@NgModule({ | ||
declarations: [ToKeyValuePairsPipe], | ||
exports: [ToKeyValuePairsPipe], | ||
}) | ||
export class NgToKeyValuePairsPipeModule { | ||
} |
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