forked from angular/angular
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(common): new pipe to support object for ngFor
Add keys_pipe which uses Object.keys in order to transform an object to array of its keys. Fix angular#2246
- Loading branch information
1 parent
0aa9b46
commit ea9ae72
Showing
7 changed files
with
101 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* @license | ||
* Copyright Google Inc. All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import { Pipe, PipeTransform } from '@angular/core'; | ||
|
||
/** | ||
* @ngModule CommonModule | ||
* @whatItDoes Transforms an object to array of its keys. | ||
* @howToUse `object_expression | keys` | ||
* @description | ||
* | ||
* Transforms an object to array of its keys using `Object.keys`. | ||
* | ||
* ### Example | ||
* {@example common/pipes/ts/keys_pipe.ts region='KeysPipe'} | ||
* | ||
* @experimental | ||
*/ | ||
@Pipe({ name: 'keys', pure: false }) | ||
export class KeysPipe implements PipeTransform { | ||
transform(value: object): Array<string> { | ||
return value != null ? Object.keys(value) : []; | ||
} | ||
} |
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 @@ | ||
/** | ||
* @license | ||
* Copyright Google Inc. All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import { KeysPipe } from '@angular/common'; | ||
|
||
export function main() { | ||
describe('KeysPipe', () => { | ||
let pipe: KeysPipe; | ||
|
||
beforeEach(() => { pipe = new KeysPipe(); }); | ||
|
||
describe('transform', () => { | ||
it('should return keys', () => { | ||
const object = { key1: 'value1', key2: 'value2' }; | ||
const keys = ['key1', 'key2']; | ||
const val = pipe.transform(object); | ||
expect(val).toEqual(keys); | ||
}); | ||
|
||
it('should return empty array if value is undefined', () => { | ||
expect(pipe.transform(undefined as any)).toEqual([]); | ||
}); | ||
|
||
it('should return empty array if value is null', () => { | ||
expect(pipe.transform(null as any)).toEqual([]); | ||
}); | ||
|
||
}); | ||
}); | ||
} |
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,23 @@ | ||
/** | ||
* @license | ||
* Copyright Google Inc. All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import {Component, NgModule} from '@angular/core'; | ||
import {BrowserModule} from '@angular/platform-browser'; | ||
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic'; | ||
|
||
// #docregion KeysPipe | ||
@Component({ | ||
selector: 'keys-pipe', | ||
template: `<div *ngFor="let key of object | keys"> | ||
key is {{key}} and value is {{object[key]}} | ||
</div>` | ||
}) | ||
export class KeysPipeComponent { | ||
object: Object = {key1: 'value1', key2: 'value2'}; | ||
} | ||
// #enddocregion |
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