-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add "keys" for getting an Object's property names
- Loading branch information
Showing
3 changed files
with
76 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/** | ||
* Get list with names of all own properties | ||
* | ||
* @tag Array,Object | ||
* @signature (source: Array|Object): string[] | ||
* | ||
* @param {Array|Object} source Array or Object to extract keys from | ||
* | ||
* @return {string[]} List of property names | ||
* | ||
* @example | ||
* keys(["lorem", "ipsum"]) | ||
* // => ["0", "1"] | ||
* | ||
* keys({ foo: "bar", lorem: "ipsum"}) | ||
* // => ["foo", "lorem"] | ||
* | ||
* keys("foo"), keys(12), keys(null), etc | ||
* // => [] | ||
*/ | ||
const keys = source => { | ||
const type = Array.isArray(source) | ||
? "array" | ||
: source !== null && typeof source === "object" | ||
? "object" | ||
: "other" | ||
|
||
switch (type) { | ||
case "object": | ||
case "array": { | ||
const entries = Object.entries(source) | ||
const result = [] | ||
|
||
for (let i = 0; i < entries.length; ++i) { | ||
result.push(entries[i][0]) | ||
} | ||
|
||
return result | ||
} | ||
default: | ||
return [] | ||
} | ||
} | ||
|
||
export { keys } |
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,30 @@ | ||
import test from "tape" | ||
import { keys } from ".." | ||
|
||
test("keys", t => { | ||
t.deepEquals( | ||
keys(["test", 1, 2]), | ||
["0", "1", "2"], | ||
"When passing an array, should return an array of string with the index of each element" | ||
) | ||
|
||
t.deepEquals( | ||
keys({ foo: "bar", lorem: "ipsum" }), | ||
["foo", "lorem"], | ||
"When passing an object, should return an array of strings with the key values" | ||
) | ||
|
||
t.deepEquals( | ||
keys("lorem"), | ||
[], | ||
"When passing a string, should return an empty array" | ||
) | ||
|
||
t.deepEquals( | ||
keys(12), | ||
[], | ||
"when passing a number, should return an empty array" | ||
) | ||
|
||
t.end() | ||
}) |