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
14ee759
commit 40b5932
Showing
6 changed files
with
93 additions
and
1 deletion.
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,27 @@ | ||
/** | ||
* @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 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,34 @@ | ||
/** | ||
* @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'; | ||
import {afterEach, beforeEach, ddescribe, describe, expect, iit, it, xit} from '@angular/core/testing/testing_internal'; | ||
|
||
export function main() { | ||
describe('KeysPipe', () => { | ||
var pipe: KeysPipe; | ||
|
||
beforeEach(() => { pipe = new KeysPipe(); }); | ||
|
||
describe('transform', () => { | ||
it('should return keys', () => { | ||
var object = {key1: 'value1', key2: 'value2'}; | ||
var keys = ['key1', 'key2']; | ||
var val = pipe.transform(object); | ||
expect(val).toEqual(keys); | ||
}); | ||
|
||
it('should throw error if value is undefined', | ||
() => { expect(() => pipe.transform(undefined)).toThrowError(); }); | ||
|
||
it('should throw error if value is null', | ||
() => { expect(() => pipe.transform(null)).toThrowError(); }); | ||
}); | ||
|
||
}); | ||
} |
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: 'json-pipe', | ||
template: `<div *ngFor="let key of object | keys"> | ||
key is {{key}} and value is {{myObject[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