-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #171 from VisActor/feat/vchart_editor_merge_spec
feat: merge spec of vchart
- Loading branch information
Showing
5 changed files
with
221 additions
and
11 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
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,78 @@ | ||
import { isObject, isValid, merge } from '@visactor/vutils'; | ||
|
||
export function set(object: any, path: string, value: any) { | ||
return object == null ? object : baseSet(object, path, value); | ||
} | ||
|
||
/** | ||
* Casts `value` to a path array if it's not one. | ||
* | ||
* @private | ||
* @param {*} value The value to inspect. | ||
* @param {Object} [object] The object to query keys on. | ||
* @returns {Array} Returns the cast property path array. | ||
*/ | ||
function castPath(path: string, object: any) { | ||
if (object[path]) { | ||
return [path]; | ||
} | ||
const arr: string[] = []; | ||
|
||
path.split('.').forEach(entry => { | ||
const res = /\[(\d)\]/g.exec(entry); | ||
|
||
if (res) { | ||
arr.push(entry.replace(res[0], '')); | ||
arr.push(res[1]); | ||
} else { | ||
arr.push(entry); | ||
} | ||
}); | ||
|
||
return arr; | ||
} | ||
|
||
function isIndex(key: string) { | ||
return /\d/.exec(key); | ||
} | ||
|
||
/** | ||
* The base implementation of `_.set`. | ||
* | ||
* @private | ||
* @param {Object} object The object to modify. | ||
* @param {Array|string} pathString The path of the property to set. | ||
* @param {*} value The value to set. | ||
* @param {Function} [customizer] The function to customize path creation. | ||
* @returns {Object} Returns `object`. | ||
*/ | ||
function baseSet(object: any, pathString: string, value: any) { | ||
if (!isObject(object)) { | ||
return object; | ||
} | ||
const path = castPath(pathString, object); | ||
|
||
let index = -1; | ||
const length = path.length; | ||
const lastIndex = length - 1; | ||
let nested: any = object; | ||
|
||
while (nested != null && ++index < length) { | ||
const key = path[index]; | ||
let newValue = value; | ||
|
||
if (index !== lastIndex) { | ||
const objValue = (nested as any)[key]; | ||
|
||
newValue = isValid(objValue) ? objValue : isIndex(path[index + 1]) ? [] : {}; | ||
} | ||
|
||
if (isValid(nested[key])) { | ||
merge(nested[key], newValue); | ||
} else { | ||
nested[key] = newValue; | ||
} | ||
nested = nested[key]; | ||
} | ||
return object; | ||
} |