super-utils-plus
Version:
A superior alternative to Lodash with improved performance, TypeScript support, and developer experience
176 lines (175 loc) • 3.43 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.entriesIn = exports.entries = void 0;
exports.keys = keys;
exports.keysIn = keysIn;
exports.values = values;
exports.valuesIn = valuesIn;
exports.toPairs = toPairs;
exports.toPairsIn = toPairsIn;
/**
* Creates an array of the own enumerable property names of object.
*
* @param object - The object to query
* @returns The array of property names
*
* @example
* ```ts
* function Foo() {
* this.a = 1;
* this.b = 2;
* }
*
* Foo.prototype.c = 3;
*
* keys(new Foo);
* // => ['a', 'b'] (iteration order is not guaranteed)
*
* keys('hi');
* // => ['0', '1']
* ```
*/
function keys(object) {
if (!object) {
return [];
}
return Object.keys(object);
}
/**
* Creates an array of the own and inherited enumerable property names of object.
*
* @param object - The object to query
* @returns The array of property names
*
* @example
* ```ts
* function Foo() {
* this.a = 1;
* this.b = 2;
* }
*
* Foo.prototype.c = 3;
*
* keysIn(new Foo);
* // => ['a', 'b', 'c'] (iteration order is not guaranteed)
* ```
*/
function keysIn(object) {
if (!object) {
return [];
}
const result = [];
for (const key in object) {
result.push(key);
}
return result;
}
/**
* Creates an array of the own enumerable string keyed property values of object.
*
* @param object - The object to query
* @returns The array of property values
*
* @example
* ```ts
* function Foo() {
* this.a = 1;
* this.b = 2;
* }
*
* Foo.prototype.c = 3;
*
* values(new Foo);
* // => [1, 2] (iteration order is not guaranteed)
*
* values('hi');
* // => ['h', 'i']
* ```
*/
function values(object) {
if (!object) {
return [];
}
return Object.values(object);
}
/**
* Creates an array of the own and inherited enumerable string keyed property
* values of object.
*
* @param object - The object to query
* @returns The array of property values
*
* @example
* ```ts
* function Foo() {
* this.a = 1;
* this.b = 2;
* }
*
* Foo.prototype.c = 3;
*
* valuesIn(new Foo);
* // => [1, 2, 3] (iteration order is not guaranteed)
* ```
*/
function valuesIn(object) {
if (!object) {
return [];
}
const result = [];
for (const key in object) {
result.push(object[key]);
}
return result;
}
/**
* Creates an array of own enumerable string keyed-value pairs for object.
*
* @param object - The object to query
* @returns The key-value pairs
*
* @example
* ```ts
* toPairs({ 'a': 1, 'b': 2 });
* // => [['a', 1], ['b', 2]]
* ```
*/
function toPairs(object) {
if (!object) {
return [];
}
return Object.entries(object);
}
/**
* Creates an array of own and inherited enumerable string keyed-value pairs
* for object.
*
* @param object - The object to query
* @returns The key-value pairs
*
* @example
* ```ts
* function Foo() {
* this.a = 1;
* this.b = 2;
* }
*
* Foo.prototype.c = 3;
*
* toPairsIn(new Foo);
* // => [['a', 1], ['b', 2], ['c', 3]] (iteration order is not guaranteed)
* ```
*/
function toPairsIn(object) {
if (!object) {
return [];
}
const result = [];
for (const key in object) {
result.push([key, object[key]]);
}
return result;
}
// Aliases
exports.entries = toPairs;
exports.entriesIn = toPairsIn;