-
-
Notifications
You must be signed in to change notification settings - Fork 21
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
5 changed files
with
93 additions
and
39 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
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,65 @@ | ||
import { isRef, unref } from 'vue' | ||
|
||
const isObject = (val: any) => val !== null && typeof val === 'object' | ||
const isArray = Array.isArray | ||
|
||
/** | ||
* Unref a value, recursing into it if it's an object. | ||
*/ | ||
const smartUnref = (val: any) => { | ||
// Non-ref object? Go deeper! | ||
if (val !== null && !isRef(val) && typeof val === 'object') | ||
// eslint-disable-next-line @typescript-eslint/no-use-before-define | ||
return deepUnref(val) | ||
|
||
return unref(val) | ||
} | ||
/** | ||
* Unref an array, recursively. | ||
* | ||
* @param {Array} arr - The array to unref. | ||
* | ||
* @return {Array} | ||
*/ | ||
const unrefArray = (arr: any) => { | ||
const unreffed: any[] = [] | ||
|
||
arr.forEach((val: any) => { | ||
unreffed.push(smartUnref(val)) | ||
}) | ||
|
||
return unreffed | ||
} | ||
|
||
/** | ||
* Unref an object, recursively. | ||
* | ||
* @param {Object} obj - The object to unref. | ||
* | ||
* @return {Object} | ||
*/ | ||
const unrefObject = (obj: any) => { | ||
const unreffed: any = {} | ||
|
||
Object.keys(obj).forEach((key) => { | ||
unreffed[key] = smartUnref(obj[key]) | ||
}) | ||
|
||
return unreffed | ||
} | ||
|
||
/** | ||
* Deeply unref a value, recursing into objects and arrays. | ||
*/ | ||
export const deepUnref = (val: any) => { | ||
const checkedVal = isRef(val) ? unref(val) : val | ||
|
||
if (!isObject(checkedVal)) | ||
return checkedVal | ||
|
||
if (isArray(checkedVal)) | ||
return unrefArray(checkedVal) | ||
|
||
return unrefObject(checkedVal) | ||
} | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.