Use Object.values if available
As `Object.values`, the arguments now can't be `null` or `undefined`.
This commit is contained in:
@@ -44,18 +44,20 @@ export function clear(object) {
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get an array of property values from an object.
|
* Polyfill for Object.values(). Get an array of property values from an object.
|
||||||
* @param {Object<K,V>} object The object from which to get the values.
|
* See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/values
|
||||||
|
*
|
||||||
|
* @param {!Object<K,V>} object The object from which to get the values.
|
||||||
* @return {!Array<V>} The property values.
|
* @return {!Array<V>} The property values.
|
||||||
* @template K,V
|
* @template K,V
|
||||||
*/
|
*/
|
||||||
export function getValues(object) {
|
export const getValues = (typeof Object.values === 'function') ? Object.values : function(object) {
|
||||||
const values = [];
|
const values = [];
|
||||||
for (const property in object) {
|
for (const property in object) {
|
||||||
values.push(object[property]);
|
values.push(object[property]);
|
||||||
}
|
}
|
||||||
return values;
|
return values;
|
||||||
}
|
};
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -58,7 +58,7 @@ describe('ol.obj.getValues()', function() {
|
|||||||
|
|
||||||
it('gets a list of property values from an object', function() {
|
it('gets a list of property values from an object', function() {
|
||||||
expect(getValues({foo: 'bar', num: 42}).sort()).to.eql([42, 'bar']);
|
expect(getValues({foo: 'bar', num: 42}).sort()).to.eql([42, 'bar']);
|
||||||
expect(getValues(null)).to.eql([]);
|
expect(getValues([])).to.eql([]);
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user