Skip to content
This repository was archived by the owner on Feb 23, 2023. It is now read-only.

Commit 08a17a9

Browse files
committed
Adds object/toKeyValuePairs
Adds a pipe that transforms objects to an array of key-value pairs.
1 parent 3bdddcf commit 08a17a9

File tree

6 files changed

+85
-2
lines changed

6 files changed

+85
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ You can find the documentations in the [`docs`](./docs) folder or on [`GitBook`]
126126
- [`keys`](./docs/object.md#keys)
127127
- [`toArray`](./docs/object.md#toarray)
128128
- [`defaults`](./docs/object.md#defaults)
129+
- [`toKeyValuePairs`](./docs/object.md#tokeyvaluepairs)
129130

130131
## Install
131132

docs/object.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
- [`keys`](#keys)
44
- [`toArray`](#toarray)
55
- [`defaults`](#defaults)
6+
- [`toKeyValuePairs`](#tokeyvaluepairs)
67

78
You can check the module import [`here`](./modules.md).
89

@@ -93,3 +94,28 @@ const array = [{ a: 2 }, null, { b: 3 }, undefined];
9394
{{ array | defaults: d }}
9495
<!-- [{ a: 2, b: 2, c: 3 }, { a: 1, b: 2, c: 3 }, { a: 1, b: 3, c: 3 }, { a: 1, b: 2, c: 3 }]-->
9596
```
97+
98+
#### toKeyValuePairs
99+
100+
Transforms an object to an array of key-value pairs.
101+
102+
##### File
103+
104+
```typescript
105+
import { NgToKeyValuePairsModule } from 'angular-pipes';
106+
```
107+
108+
##### Usage
109+
110+
```javascript
111+
const value = {
112+
a: 1,
113+
b: 2,
114+
c: 3,
115+
};
116+
```
117+
118+
```html
119+
{{ object | toKeyValuePairs }}
120+
<!-- [{key: 'a', value: 1}, {key: 'b', value: 2}, {key: 'c', value: 3}] -->
121+
```

src/object/object.module.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ import { NgModule } from '@angular/core';
33
import { NgKeysPipeModule } from './keys.pipe';
44
import { NgToArrayPipeModule } from './to-array.pipe';
55
import { NgDefaultsPipeModule } from './defaults.pipe';
6+
import { NgToKeyValuePairsPipeModule } from './to-key-value-pairs';
67

78
@NgModule({
8-
imports: [NgKeysPipeModule, NgToArrayPipeModule, NgDefaultsPipeModule],
9+
imports: [NgKeysPipeModule, NgToArrayPipeModule, NgDefaultsPipeModule, NgToKeyValuePairsPipeModule],
910
})
10-
export class NgObjectPipesModule {}
11+
export class NgObjectPipesModule {
12+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { ToKeyValuePairsPipe } from './to-key-value-pairs';
2+
3+
describe('ToKeyValuePairsPipe', () => {
4+
let pipe: ToKeyValuePairsPipe;
5+
6+
beforeEach(() => {
7+
pipe = new ToKeyValuePairsPipe();
8+
});
9+
10+
const value = {
11+
a: 1,
12+
b: 2,
13+
c: 3,
14+
};
15+
16+
it('should transform the object to an array of KVPs', () => {
17+
expect(pipe.transform(value)).toEqual([
18+
{ key: 'a', value: 1 }, { key: 'b', value: 2 }, { key: 'c', value: 3 },
19+
]);
20+
});
21+
22+
it('should return non-object input unchanged', () => {
23+
expect(pipe.transform('input')).toEqual('input');
24+
});
25+
});

src/object/to-key-value-pairs.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { NgModule, Pipe, PipeTransform } from '@angular/core';
2+
import { isObject } from '../utils/utils';
3+
4+
/**
5+
* @description Transforms an object to an array of key/value pairs,
6+
* returns the input unchanged when not of type object.
7+
*/
8+
@Pipe({ name: 'toKeyValuePairs' })
9+
export class ToKeyValuePairsPipe implements PipeTransform {
10+
transform(input: any): any {
11+
12+
// Any input not of type object is returned un-mutated.
13+
if (!isObject(input)) {
14+
return input;
15+
}
16+
17+
// Get the array of key/values of the object and transform into an array of KVPs
18+
return Object.entries(input).map(element => ({ key: element[0], value: element[1] }));
19+
}
20+
21+
}
22+
23+
@NgModule({
24+
declarations: [ToKeyValuePairsPipe],
25+
exports: [ToKeyValuePairsPipe],
26+
})
27+
export class NgToKeyValuePairsPipeModule {
28+
}

src/public_api.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ export * from './math/ordinal.pipe';
7474
export * from './object/keys.pipe';
7575
export * from './object/to-array.pipe';
7676
export * from './object/defaults.pipe';
77+
export * from './object/to-key-value-pairs';
7778

7879
export * from './string/left-pad.pipe';
7980
export * from './string/match.pipe';

0 commit comments

Comments
 (0)