refactor: remove keys() and values() from MapWrapper

This commit is contained in:
Victor Berchet
2016-11-03 16:58:27 -07:00
committed by vikerman
parent 121e5080aa
commit ec92f4b198
19 changed files with 138 additions and 216 deletions

View File

@ -6,39 +6,16 @@
* found in the LICENSE file at https://angular.io/license
*/
import {getSymbolIterator, isBlank, isJsObject, isPresent} from './lang';
// Safari doesn't implement MapIterator.next(), which is used is Traceur's polyfill of Array.from
// TODO(mlaval): remove the work around once we have a working polyfill of Array.from
const _arrayFromMap: {(m: Map<any, any>, getValues: boolean): any[]} = (function() {
try {
if ((<any>(new Map()).values()).next) {
return function createArrayFromMap(m: Map<any, any>, getValues: boolean): any[] {
return getValues ? (<any>Array).from(m.values()) : (<any>Array).from(m.keys());
};
}
} catch (e) {
}
return function createArrayFromMapWithForeach(m: Map<any, any>, getValues: boolean): any[] {
var res = new Array(m.size), i = 0;
m.forEach((v, k) => {
res[i] = getValues ? v : k;
i++;
});
return res;
};
})();
import {getSymbolIterator, isJsObject, isPresent} from './lang';
export class MapWrapper {
static createFromStringMap<T>(stringMap: {[key: string]: T}): Map<string, T> {
var result = new Map<string, T>();
for (var prop in stringMap) {
const result = new Map<string, T>();
for (let prop in stringMap) {
result.set(prop, stringMap[prop]);
}
return result;
}
static keys<K>(m: Map<K, any>): K[] { return _arrayFromMap(m, false); }
static values<V>(m: Map<any, V>): V[] { return _arrayFromMap(m, true); }
}
/**

View File

@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/
import {ListWrapper, MapWrapper, StringMapWrapper} from '../src/collection';
import {ListWrapper, StringMapWrapper} from '../src/collection';
export function main() {
describe('ListWrapper', () => {
@ -35,45 +35,36 @@ export function main() {
() => { expect(StringMapWrapper.equals({}, {})).toBe(true); });
it('should return true when comparing the same map', () => {
var m1: {[key: string]: number} = {'a': 1, 'b': 2, 'c': 3};
const m1: {[key: string]: number} = {'a': 1, 'b': 2, 'c': 3};
expect(StringMapWrapper.equals(m1, m1)).toBe(true);
});
it('should return true when comparing different maps with the same keys and values', () => {
var m1: {[key: string]: number} = {'a': 1, 'b': 2, 'c': 3};
var m2: {[key: string]: number} = {'a': 1, 'b': 2, 'c': 3};
const m1: {[key: string]: number} = {'a': 1, 'b': 2, 'c': 3};
const m2: {[key: string]: number} = {'a': 1, 'b': 2, 'c': 3};
expect(StringMapWrapper.equals(m1, m2)).toBe(true);
});
it('should return false when comparing maps with different numbers of keys', () => {
var m1: {[key: string]: number} = {'a': 1, 'b': 2, 'c': 3};
var m2: {[key: string]: number} = {'a': 1, 'b': 2, 'c': 3, 'd': 4};
const m1: {[key: string]: number} = {'a': 1, 'b': 2, 'c': 3};
const m2: {[key: string]: number} = {'a': 1, 'b': 2, 'c': 3, 'd': 4};
expect(StringMapWrapper.equals(m1, m2)).toBe(false);
expect(StringMapWrapper.equals(m2, m1)).toBe(false);
});
it('should return false when comparing maps with different keys', () => {
var m1: {[key: string]: number} = {'a': 1, 'b': 2, 'c': 3};
var m2: {[key: string]: number} = {'a': 1, 'b': 2, 'CC': 3};
const m1: {[key: string]: number} = {'a': 1, 'b': 2, 'c': 3};
const m2: {[key: string]: number} = {'a': 1, 'b': 2, 'CC': 3};
expect(StringMapWrapper.equals(m1, m2)).toBe(false);
expect(StringMapWrapper.equals(m2, m1)).toBe(false);
});
it('should return false when comparing maps with different values', () => {
var m1: {[key: string]: number} = {'a': 1, 'b': 2, 'c': 3};
var m2: {[key: string]: number} = {'a': 1, 'b': 20, 'c': 3};
const m1: {[key: string]: number} = {'a': 1, 'b': 2, 'c': 3};
const m2: {[key: string]: number} = {'a': 1, 'b': 20, 'c': 3};
expect(StringMapWrapper.equals(m1, m2)).toBe(false);
expect(StringMapWrapper.equals(m2, m1)).toBe(false);
});
});
describe('MapWrapper', () => {
it('should return a list of keys values', () => {
var m = new Map();
m.set('a', 'b');
expect(MapWrapper.keys(m)).toEqual(['a']);
expect(MapWrapper.values(m)).toEqual(['b']);
});
});
});
}