-
Notifications
You must be signed in to change notification settings - Fork 4
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
3 changed files
with
58 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import type { Dictionary } from '../type'; | ||
|
||
type ToObjectArrayOptions = { | ||
/** | ||
* @default value | ||
*/ | ||
keyPropName?: string; | ||
/** | ||
* @default label | ||
*/ | ||
valuePropName?: string; | ||
}; | ||
|
||
/** | ||
* object to array | ||
* @param dict {Dictionary<any>} | ||
* @param opts {ToObjectArrayOptions} | ||
* @default { keyPropName: 'label', valuePropName: 'value' } | ||
* @example | ||
* ```ts | ||
* const a = { a: 1, b: 2 } | ||
* toObjectArray(a) // [{ value: 'a', label: 1 }, { value: 'b', label: 2 }] | ||
* ``` | ||
* @category Object | ||
*/ | ||
function toObjectArray<T extends Record<string, any> = { label: any; value: string }>( | ||
dict: Dictionary<any>, | ||
opts?: ToObjectArrayOptions, | ||
): Array<T> { | ||
const { keyPropName = 'value', valuePropName = 'label' } = opts || {}; | ||
const result: any[] = []; | ||
for (const k in dict) { | ||
result.push({ | ||
[keyPropName]: k, | ||
[valuePropName]: dict[k], | ||
}); | ||
} | ||
return result; | ||
} | ||
|
||
export default toObjectArray; |
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,15 @@ | ||
/** | ||
* 转换成对象 | ||
* @param value | ||
* @category Object | ||
*/ | ||
function toPlanObject(value?: any): Object { | ||
value = Object(value); | ||
const result = {}; | ||
for (const k in value) { | ||
result[k] = value[k]; | ||
} | ||
return result; | ||
} | ||
|
||
export default toPlanObject; |