Skip to content

Commit d63c415

Browse files
authored
Merge pull request #8 from Feresaul/filter
Filter Utility Updates & Tests
2 parents 3746572 + ee1f30e commit d63c415

7 files changed

Lines changed: 164 additions & 50 deletions

File tree

.github/workflows/verify-build.yml

Lines changed: 0 additions & 28 deletions
This file was deleted.

README.md

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
# @nuc-lib/deep-key
2-
3-
## Description
4-
5-
`@nuc-lib/deep-key` is a utility library designed to simplify working with deeply nested objects in JavaScript. This is intended to be used when you work with dynamic keys or when you are not sure if the property exists. It provides a set of functions so you don't have to deal with the complexity of checking for the existence of properties at each level of the object.
1+
A utility library designed to simplify working with deeply nested objects in TypeScript. This is intended to be used when you work with dynamic keys or when you are not sure if the property exists. It provides a set of functions so you don't have to deal with the complexity of checking for the existence of properties at each level of the object.
62

73
## Features
84

@@ -24,7 +20,11 @@ import { getKeyValue } from '@nuc-lib/deep-key';
2420

2521
const guy = {
2622
id: 2,
27-
personalInfo: { name: 'John Doe', age: 12, city: 'New York' },
23+
personalInfo: {
24+
name: 'John Doe',
25+
age: 12,
26+
city: 'New York'
27+
},
2828
contacts: [
2929
{ name: 'Jane Doe', email: 'afk@example.com' },
3030
{ name: 'Alice Smith', email: 'alice@example.com' },
@@ -42,7 +42,7 @@ getKeyValue(guy, 'personalInfo.active'); // undefined -> no error thrown
4242

4343
For arrays there are two ways to access the values:
4444

45-
- Getting an element in a specific index.
45+
- **Getting an element in a specific index.**
4646

4747
```javascript
4848
getKeyValue(guy, 'contacts.0');
@@ -51,7 +51,8 @@ getKeyValue(guy, 'contacts.0.name'); // 'Jane Doe'
5151
getKeyValue(guy, 'contacts.0.email'); // 'afk@example.com'
5252
```
5353

54-
- Getting all the values in the array. A key wrapped in `[]` represents an actual mapping over the array, this means it will return an array of values from the parent array.
54+
- **Getting all the values in the array.**
55+
A key wrapped in `[]` represents an actual mapping over the array, this means it will return an array of values from the parent array.
5556

5657
> **Note:** There is no need to use the `[]` notation if you are not using TypeScript. The utility will still map over the array and return the values. This is just a visual aid to show that the key is an array to be mapped.
5758
@@ -118,10 +119,7 @@ Strict mode for filter arrays is also supported. This means that the filter will
118119

119120
```javascript
120121
filterByKeyValue(people, 'age', [22, 25]);
121-
// [
122-
// { id: 1, name: 'John Doe', age: 25, parentIds: [ 1, 2 ], address: { city: 'Houston', zip: '10001' } },
123-
// { id: 3, name: 'Alice Smith', age: 22, parentIds: [5, 6], address: { city: 'Chicago', zip: '60601' } },
124-
// ]
122+
// []
125123
```
126124

127125
You can do a **loose filter** by passing `false` as the fourth argument.

lib/utils/filter.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ export const filterByKeyValue = <T extends TObject>(
4949
return filter.some((val) => itemKeyValue.includes(val));
5050
}
5151

52+
if (strict) {
53+
return filter.every((val) => val === itemKeyValue);
54+
}
55+
// If strict is false, check if any of the filter values are in the itemKeyValue
5256
return filter.some((val) => val === itemKeyValue);
5357
}
5458
// If key value is an array check if filter is in the array

package.json

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@
66
"license": "MIT",
77
"type": "module",
88
"scripts": {
9-
"build": "rimraf dist && tsc --project tsconfig.json",
109
"lint": "eslint . --ext .ts",
11-
"test": "vitest",
12-
"prepublishOnly": "npm run build"
10+
"test": "vitest"
1311
},
1412
"keywords": [
1513
"typescript",
@@ -27,13 +25,12 @@
2725
"homepage": "https://github.com/Feresaul/deep-key#readme",
2826
"exports": {
2927
".": {
30-
"default": "./dist/index.js",
31-
"types": "./dist/index.d.ts"
28+
"import": "./lib/index.ts"
3229
},
3330
"./package.json": "./package.json"
3431
},
3532
"files": [
36-
"dist"
33+
"lib"
3734
],
3835
"devDependencies": {
3936
"@eslint/js": "^9.26.0",

tests/filter.test.ts

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
import { describe, it, expect } from 'vitest';
2+
3+
import { filterByKeyValue } from '../lib/utils/filter';
4+
5+
const people = [
6+
{
7+
id: 1,
8+
name: 'John Doe',
9+
age: 25,
10+
parentIds: [1, 2],
11+
address: { city: 'Houston', zip: '10001' }
12+
},
13+
{
14+
id: 2,
15+
name: 'Jane Doe',
16+
age: 30,
17+
parentIds: [3, 4],
18+
address: { city: 'Los Angeles', zip: '90001' }
19+
},
20+
{
21+
id: 3,
22+
name: 'Alice Smith',
23+
age: 22,
24+
parentIds: [5, 6],
25+
address: { city: 'Chicago', zip: '60601' }
26+
},
27+
{
28+
id: 4,
29+
name: 'Bob Johnson',
30+
age: 28,
31+
parentIds: [7, 8],
32+
address: { city: 'Houston', zip: '77001' }
33+
}
34+
];
35+
36+
describe('filterByKeyValue', () => {
37+
it('should filter array by filter value', () => {
38+
expect(filterByKeyValue(people, 'age', 30)).toEqual([
39+
{
40+
id: 2,
41+
name: 'Jane Doe',
42+
age: 30,
43+
parentIds: [3, 4],
44+
address: { city: 'Los Angeles', zip: '90001' }
45+
}
46+
]);
47+
});
48+
49+
it('should filter array by filter value - array strict', () => {
50+
expect(filterByKeyValue(people, 'age', [22, 25])).toEqual([]);
51+
});
52+
53+
it('should filter array by filter value - array loose', () => {
54+
expect(filterByKeyValue(people, 'age', [22, 25], false)).toEqual([
55+
{
56+
id: 1,
57+
name: 'John Doe',
58+
age: 25,
59+
parentIds: [1, 2],
60+
address: { city: 'Houston', zip: '10001' }
61+
},
62+
{
63+
id: 3,
64+
name: 'Alice Smith',
65+
age: 22,
66+
parentIds: [5, 6],
67+
address: { city: 'Chicago', zip: '60601' }
68+
}
69+
]);
70+
});
71+
72+
it('should filter array by filter value - array value strict', () => {
73+
expect(filterByKeyValue(people, 'parentIds', [1, 8])).toEqual([]);
74+
});
75+
76+
it('should filter array by filter value - array value loose', () => {
77+
expect(filterByKeyValue(people, 'parentIds', [1, 8], false)).toEqual([
78+
{
79+
id: 1,
80+
name: 'John Doe',
81+
age: 25,
82+
parentIds: [1, 2],
83+
address: { city: 'Houston', zip: '10001' }
84+
},
85+
{
86+
id: 4,
87+
name: 'Bob Johnson',
88+
age: 28,
89+
parentIds: [7, 8],
90+
address: { city: 'Houston', zip: '77001' }
91+
}
92+
]);
93+
});
94+
95+
it('should filter array by filter value - custom filter', () => {
96+
expect(
97+
filterByKeyValue(people, 'age', (value) => Number(value) > 25)
98+
).toEqual([
99+
{
100+
id: 2,
101+
name: 'Jane Doe',
102+
age: 30,
103+
parentIds: [3, 4],
104+
address: { city: 'Los Angeles', zip: '90001' }
105+
},
106+
{
107+
id: 4,
108+
name: 'Bob Johnson',
109+
age: 28,
110+
parentIds: [7, 8],
111+
address: { city: 'Houston', zip: '77001' }
112+
}
113+
]);
114+
});
115+
116+
it('should filter array by filter value - nested key', () => {
117+
expect(filterByKeyValue(people, 'address.city', 'Los Angeles')).toEqual(
118+
[
119+
{
120+
id: 2,
121+
name: 'Jane Doe',
122+
age: 30,
123+
parentIds: [3, 4],
124+
address: { city: 'Los Angeles', zip: '90001' }
125+
}
126+
]
127+
);
128+
expect(filterByKeyValue(people, 'address.city', 'Houston')).toEqual([
129+
{
130+
id: 1,
131+
name: 'John Doe',
132+
age: 25,
133+
parentIds: [1, 2],
134+
address: { city: 'Houston', zip: '10001' }
135+
},
136+
{
137+
id: 4,
138+
name: 'Bob Johnson',
139+
age: 28,
140+
parentIds: [7, 8],
141+
address: { city: 'Houston', zip: '77001' }
142+
}
143+
]);
144+
});
145+
});

tests/value.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, it, expect } from 'vitest';
22

3-
import { getKeyValue } from '@nuc-lib/deep-key';
3+
import { getKeyValue } from '../lib/utils/value';
44

55
const guy = {
66
id: 2,

tsconfig.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
"module": "ESNext",
66
"esModuleInterop": true,
77
"forceConsistentCasingInFileNames": true,
8-
"noEmit": false,
9-
"declaration": true,
10-
"outDir": "dist",
8+
"noEmit": true,
119
"moduleResolution": "bundler",
1210
// Best practices
1311
"strict": true,

0 commit comments

Comments
 (0)