Skip to content

Commit

Permalink
Initial release
Browse files Browse the repository at this point in the history
  • Loading branch information
wendtcode committed Mar 7, 2019
1 parent 8b929fb commit d1b85bd
Show file tree
Hide file tree
Showing 6 changed files with 9,527 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,6 @@ typings/

# next.js build output
.next

# bundle
dist
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,26 @@
# inso
Insensitive object search

:mag_right: Case-insensitive object search

### Install

`npm install inso`

### Usage

```javascript
const inso = require("inso");

const obj = {
foo: 1,
BAR: 2,
bAz: 3
};

inso(); // undefined
inso(obj); // undefined
inso(obj, "foo"); // 1
inso(obj, "FOO"); // 1
inso(obj, "Foo"); // 1
inso(obj, "a", "b", "c", "Bar"); // 2
```
25 changes: 25 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export default function inso(obj, ...searches) {
if (!obj || !searches.length) {
return void 0;
}

let match;

const objKeys = Object.keys(obj);
const lowerObjKeys = objKeys.map(objKey => objKey.toLowerCase());
const fuzzyKeyIndex = lowerObjKeys.findIndex(lowerObjKey =>
searches.some(search => search.toLowerCase() === lowerObjKey)
);

const fuzzyKey = fuzzyKeyIndex > -1 && objKeys[fuzzyKeyIndex];
if (fuzzyKey) {
match = obj[fuzzyKey];
}

const exactKey = searches.filter(search => obj[search])[0];
if (exactKey) {
match = obj[exactKey];
}

return match;
}

0 comments on commit d1b85bd

Please sign in to comment.