-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
354 additions
and
137 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
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{ | ||
"name": "js-cool", | ||
"description": "Collection of common JavaScript / TypeScript utilities", | ||
"version": "5.14.1", | ||
"version": "5.15.0", | ||
"packageManager": "[email protected]", | ||
"main": "dist/index.cjs.js", | ||
"module": "dist/index.esm-bundler.js", | ||
|
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,69 @@ | ||
import isArray from './isArray' | ||
import isDate from './isDate' | ||
import isRegExp from './isRegExp' | ||
|
||
/** | ||
* deep clone (Buffer, Promise, Set, Map are not supported) | ||
* | ||
* @example | ||
* ```js | ||
* const source = { a: 100, reg: /\d+/g, arr: [1, 2] } | ||
* const res = clone(source) | ||
* // { a: 100, reg: /\d+/g, arr: [1, 2] } | ||
* ``` | ||
* @since 5.15.0 | ||
* @param parent - source object | ||
* @returns - new object | ||
*/ | ||
function clone<T = any>(parent: T): T { | ||
// handle regexp | ||
const getRegExp = (reg: RegExp) => { | ||
let flags = '' | ||
if (reg.global) flags += 'g' | ||
if (reg.ignoreCase) flags += 'i' | ||
if (reg.multiline) flags += 'm' | ||
|
||
return flags | ||
} | ||
// Maintain two arrays of circular references | ||
const parents: any[] = [] | ||
const children: any[] = [] | ||
|
||
const _clone = <P = any>(parent: P): P => { | ||
if (parent === null || typeof parent !== 'object') return parent | ||
|
||
let child, proto | ||
|
||
if (isArray(parent)) { | ||
child = [] | ||
} else if (isRegExp(parent)) { | ||
child = new RegExp(parent.source, getRegExp(parent)) | ||
if (parent.lastIndex) child.lastIndex = parent.lastIndex | ||
} else if (isDate(parent)) { | ||
child = new Date(parent.getTime()) | ||
} else { | ||
proto = Object.getPrototypeOf(parent) | ||
child = Object.create(proto) | ||
} | ||
|
||
// Handling circular references | ||
const index = parents.indexOf(parent) | ||
|
||
// If this object exists in the parent array, it has already been referenced, so return it directly. | ||
if (index !== -1) return children[index] | ||
|
||
parents.push(parent) | ||
children.push(child) | ||
|
||
for (const i in parent) { | ||
// recursive | ||
child[i] = _clone(parent[i]) | ||
} | ||
|
||
return child | ||
} | ||
|
||
return _clone(parent) | ||
} | ||
|
||
export default clone |
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
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
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,21 @@ | ||
import getType from './getType' | ||
|
||
/** | ||
* Determine if target is Date | ||
* | ||
* @example | ||
* ```js | ||
* const now = new Date() | ||
* | ||
* isDate(now) | ||
* // true | ||
* ``` | ||
* @since 5.15.0 | ||
* @param target - any target | ||
* @returns - target is Date | ||
*/ | ||
function isDate(target: any): target is Date { | ||
return target && getType(target) === 'date' | ||
} | ||
|
||
export default isDate |
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,18 @@ | ||
import getType from './getType' | ||
|
||
/** | ||
* Determine if target is RegExp | ||
* | ||
* @example | ||
* ```js | ||
* isRegExp(/\d/) // true | ||
* ``` | ||
* @since 5.15.0 | ||
* @param target - any target | ||
* @returns - target is RegExp | ||
*/ | ||
function isRegExp(target: any): target is RegExp { | ||
return target && getType(target) === 'regexp' | ||
} | ||
|
||
export default isRegExp |