Skip to content

Commit 6e6560d

Browse files
committed
feat: introduce ox
1 parent 0c0c8f2 commit 6e6560d

File tree

10 files changed

+3405
-3305
lines changed

10 files changed

+3405
-3305
lines changed

.github/workflows/pull_requests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: Pull Requests
33
on:
44
pull_request:
55
branches:
6-
- master
6+
- main
77

88
jobs:
99
test:

.husky/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
_

.husky/pre-commit

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/sh
2+
. "$(dirname "$0")/_/husky.sh"
3+
4+
yarn tsdx lint

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2019 Rohman Habib M
3+
Copyright (c) 2021 Rohman Habib M
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,52 @@
1-
# purify-obj
1+
# @rohmanhm/ox
22

3-
> Purify & clean objects recursively, deleting undefined & null or falsy properties.
3+
> A Javascript utility to clean objects recursively, deleting undefined & null or falsy properties.
44
55
## Install
66

77
```
8-
$ npm install purify-obj
8+
$ npm install @rohmanhm/ox
99
```
1010

1111
Or
1212

1313
```
14-
$ yarn add purify-obj
14+
$ yarn add @rohmanhm/ox
1515
```
1616

1717
## Usage
1818

1919
```js
20-
import { PurifyObj } from "purify-obj";
20+
import ox from '@rohmanhm/ox';
2121
// Or
22-
const { PurifyObj } = require("purify-obj");
23-
// Or you can use the `po` shortcut
24-
import { po } from "purify-obj";
22+
const ox = require('@rohmanhm/ox').default;
2523

2624
const object = {
27-
className: "only-this-obj-key-will-persist",
28-
disabled: "",
25+
className: 'only-this-obj-key-will-persist',
26+
disabled: '',
2927
autoComplete: false,
3028
checked: undefined,
31-
spellCheck: null
29+
spellCheck: null,
3230
};
33-
const newObject = po(object);
31+
const newObject = ox(object);
3432
//=> {className: 'only-this-obj-key-will-persist'}
3533
```
3634

3735
## API
3836

39-
### PurifyObj(source, strict)
37+
### ox(source, strict)
4038

4139
#### source
4240

4341
Type: `object`
4442

45-
Source object to purify properties from.
43+
Source object to clean properties from.
4644

4745
#### strict
4846

4947
Type: `boolean`
50-
51-
Strict will purify all falsy key
48+
Default: `true`
49+
Strict will clean all falsy key
5250

5351
## Local Development
5452

package.json

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
{
2-
"name": "purify-obj",
2+
"name": "@rohmanhm/ox",
33
"version": "0.1.0",
44
"license": "MIT",
55
"main": "dist/index.js",
6-
"module": "dist/purify-obj.esm.js",
6+
"module": "dist/ox.esm.js",
77
"typings": "dist/index.d.ts",
8-
"repository": "rohmanhm/purify-obj",
9-
"description": "Purify & clean objects recursively, deleting undefined & null or falsy properties.",
8+
"repository": "rohmanhm/ox",
9+
"description": "A Javascript utility to clean objects recursively, deleting undefined & null or falsy properties. ",
1010
"author": {
1111
"name": "Rohman Habib M",
1212
"email": "rohmanhm@icloud.com",
13-
"url": "https://rohmanhm.github.com"
13+
"url": "https://rohmanhm.com"
1414
},
1515
"keywords": [
16+
"utility",
1617
"purify",
1718
"clean",
1819
"object",
@@ -30,25 +31,24 @@
3031
"start": "tsdx watch",
3132
"build": "tsdx build",
3233
"test": "tsdx test",
33-
"lint": "tsdx lint"
34+
"lint": "tsdx lint",
35+
"postinstall": "husky install",
36+
"prepublishOnly": "pinst --disable",
37+
"postpublish": "pinst --enable"
3438
},
3539
"peerDependencies": {},
36-
"husky": {
37-
"hooks": {
38-
"pre-commit": "tsdx lint"
39-
}
40-
},
4140
"prettier": {
4241
"printWidth": 80,
4342
"semi": true,
4443
"singleQuote": true,
4544
"trailingComma": "es5"
4645
},
4746
"devDependencies": {
48-
"@types/jest": "^24.0.23",
49-
"husky": "^3.1.0",
50-
"tsdx": "^0.11.0",
51-
"tslib": "^1.10.0",
52-
"typescript": "^3.7.2"
47+
"@types/jest": "^26.0.20",
48+
"husky": "5",
49+
"pinst": "^2.1.4",
50+
"tsdx": "^0.14.1",
51+
"tslib": "^2.1.0",
52+
"typescript": "^4.1.5"
5353
}
5454
}

src/index.ts

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { isEmpty } from './isEmpty';
22

33
// Type of object shape
4-
export type ObjectShape = { [key: string]: any };
4+
export type Obj = { [key: string]: any };
55
// Type of ignored type
66
export type IgnoredTypes = null | undefined;
77
// Util to extract type from <T> value
@@ -11,34 +11,14 @@ export type MethodKeys<T, U> = {
1111
// Util to remove ignored key
1212
export type RemoveIgnoredKey<T> = Pick<T, MethodKeys<T, IgnoredTypes>> | null;
1313
// Type object return
14-
export type PurifyObjReturn<ObjectType> = RemoveIgnoredKey<ObjectType>;
14+
export type OxReturn<ObjectType> = RemoveIgnoredKey<ObjectType>;
1515

1616
const toStr = Object.prototype.toString;
1717

18-
export function PurifyObj<ObjectType extends ObjectShape>(
19-
obj: ObjectType,
20-
strict?: boolean
21-
): PurifyObjReturn<ObjectType> {
22-
strict = strict || false;
23-
24-
let newObj = _purifyObj(obj, strict);
25-
26-
if (newObj === null) {
27-
newObj = {} as any;
28-
}
29-
30-
return newObj;
31-
}
32-
33-
export const po = PurifyObj;
34-
35-
function _purifyObj<ObjectType extends ObjectShape>(
36-
obj: ObjectType,
37-
strict: boolean
38-
): PurifyObjReturn<ObjectType> {
18+
function _ox<T extends Obj>(obj: T, strict: boolean): OxReturn<T> {
3919
let k: string;
4020
for (k in obj) {
41-
purifyProperty(k, obj[k], obj);
21+
cleanProperty(k, obj[k], obj);
4222
}
4323

4424
// Check if the object is empty
@@ -49,8 +29,8 @@ function _purifyObj<ObjectType extends ObjectShape>(
4929
return null;
5030
}
5131

52-
function purifyProperty(key: string, value: any, ref: ObjectShape) {
53-
if (!shouldPurifyProperty(value)) {
32+
function cleanProperty(key: string, value: any, ref: Obj) {
33+
if (!shouldCleanProperty(value)) {
5434
delete ref[key];
5535
return;
5636
}
@@ -61,9 +41,9 @@ function _purifyObj<ObjectType extends ObjectShape>(
6141
if (typeOfValue === 'object' && toStr.call(value) !== '[object Date]') {
6242
// Exception - If array
6343
if (toStr.call(obj[k]) === '[object Array]') {
64-
ref[key] = ref[key].filter(shouldPurifyProperty);
44+
ref[key] = ref[key].filter(shouldCleanProperty);
6545
} else {
66-
_purifyObj(ref[key], strict);
46+
_ox(ref[key], strict);
6747
}
6848

6949
if (isEmpty(ref[key])) {
@@ -72,9 +52,23 @@ function _purifyObj<ObjectType extends ObjectShape>(
7252
}
7353
}
7454

75-
function shouldPurifyProperty(value: any) {
55+
function shouldCleanProperty(value: any) {
7656
return !(!value && (strict || (!strict && value == null)));
7757
}
7858
}
7959

60+
export default function ox<T extends Obj>(
61+
obj: T,
62+
strict?: boolean
63+
): OxReturn<T> {
64+
strict = typeof strict === 'undefined' ? true : strict;
65+
let newObj = _ox(obj, strict);
66+
67+
if (newObj === null) {
68+
newObj = {} as any;
69+
}
70+
71+
return newObj;
72+
}
73+
8074
export { isEmpty };

test/index.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
import { po } from '../src';
1+
import ox from '../src';
22

33
test('remove null and undefined key value', () => {
44
const expected = {
55
className: 'only-this-obj-key-will-persist',
6-
disabled: '',
7-
autoComplete: false,
86
};
97

10-
const actual = po({
8+
const actual = ox({
119
...expected,
1210
disabled: '',
1311
autoComplete: false,
@@ -18,20 +16,22 @@ test('remove null and undefined key value', () => {
1816
expect(actual).toEqual(expected);
1917
});
2018

21-
test('strict mode remove falsy key value', () => {
19+
test('disable strict mode not remove falsy key value', () => {
2220
const expected = {
2321
className: 'only-this-obj-key-will-persist',
22+
disabled: '',
23+
autoComplete: false,
2424
};
2525

26-
const actual = po(
26+
const actual = ox(
2727
{
2828
...expected,
2929
disabled: '',
3030
autoComplete: false,
3131
checked: undefined,
3232
spellCheck: null,
3333
},
34-
true
34+
false
3535
);
3636

3737
expect(actual).toEqual(expected);

tsconfig.json

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,35 @@
11
{
2-
"include": ["src", "types", "test"],
2+
// see https://www.typescriptlang.org/tsconfig to better understand tsconfigs
3+
"include": ["src", "types"],
34
"compilerOptions": {
4-
"target": "es5",
55
"module": "esnext",
66
"lib": ["dom", "esnext"],
77
"importHelpers": true,
8+
// output .d.ts declaration files for consumers
89
"declaration": true,
10+
// output .js.map sourcemap files for consumers
911
"sourceMap": true,
10-
"rootDir": "./",
12+
// match output dir to input dir. e.g. dist/index instead of dist/src/index
13+
"rootDir": "./src",
14+
// stricter type-checking for stronger correctness. Recommended by TS
1115
"strict": true,
12-
"strictNullChecks": true,
13-
"strictFunctionTypes": true,
14-
"strictPropertyInitialization": true,
15-
"noImplicitThis": true,
16-
"alwaysStrict": true,
17-
"noUnusedLocals": true,
18-
"noUnusedParameters": true,
16+
// linter checks for common issues
1917
"noImplicitReturns": true,
2018
"noFallthroughCasesInSwitch": true,
19+
// noUnused* overlap with @typescript-eslint/no-unused-vars, can disable if duplicative
20+
"noUnusedLocals": true,
21+
"noUnusedParameters": true,
22+
// use Node's module resolution algorithm, instead of the legacy TS one
2123
"moduleResolution": "node",
22-
"baseUrl": "./",
23-
"esModuleInterop": true
24+
// transpile JSX to React.createElement
25+
"jsx": "react",
26+
// interop between ESM and CJS modules. Recommended by TS
27+
"esModuleInterop": true,
28+
// significant perf increase by skipping checking .d.ts files, particularly those in node_modules. Recommended by TS
29+
"skipLibCheck": true,
30+
// error out if import and file system have a casing mismatch. Recommended by TS
31+
"forceConsistentCasingInFileNames": true,
32+
// `tsdx build` ignores this option, but it is commonly used when type-checking separately with `tsc`
33+
"noEmit": true
2434
}
2535
}

0 commit comments

Comments
 (0)