-
-
Notifications
You must be signed in to change notification settings - Fork 42
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
1 parent
4db51b6
commit dc5bb73
Showing
5 changed files
with
158 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
declare namespace mapObject { | ||
type Mapper< | ||
SourceObjectType extends {[key: string]: unknown}, | ||
MappedObjectKeyType extends string, | ||
MappedObjectValueType extends unknown | ||
> = ( | ||
sourceKey: keyof SourceObjectType, | ||
sourceValue: SourceObjectType[keyof SourceObjectType], | ||
source: SourceObjectType | ||
) => [MappedObjectKeyType, MappedObjectValueType]; | ||
|
||
interface Options { | ||
/** | ||
Recurse nested objects and objects in arrays. | ||
@default false | ||
*/ | ||
deep?: boolean; | ||
|
||
/** | ||
Target object to map properties on to. | ||
@default {} | ||
*/ | ||
target?: {[key: string]: unknown}; | ||
} | ||
|
||
interface DeepOptions extends Options { | ||
deep: true; | ||
} | ||
|
||
interface TargetOptions<TargetObjectType extends {[key: string]: unknown}> | ||
extends Options { | ||
target: TargetObjectType; | ||
} | ||
} | ||
|
||
/** | ||
Map object keys and values into a new object. | ||
@param source - Source object to copy properties from. | ||
@param mapper - Mapping function. | ||
@example | ||
``` | ||
import mapObject = require('map-obj'); | ||
const newObject = mapObject({foo: 'bar'}, (key, value) => [value, key]); | ||
//=> {bar: 'foo'} | ||
``` | ||
*/ | ||
declare function mapObject< | ||
SourceObjectType extends object, | ||
TargetObjectType extends {[key: string]: unknown}, | ||
MappedObjectKeyType extends string, | ||
MappedObjectValueType extends unknown = unknown | ||
>( | ||
source: SourceObjectType, | ||
mapper: mapObject.Mapper< | ||
SourceObjectType, | ||
MappedObjectKeyType, | ||
MappedObjectValueType | ||
>, | ||
options: mapObject.DeepOptions & mapObject.TargetOptions<TargetObjectType> | ||
): TargetObjectType & {[key: string]: unknown}; | ||
declare function mapObject< | ||
SourceObjectType extends object, | ||
MappedObjectKeyType extends string, | ||
MappedObjectValueType extends unknown = unknown | ||
>( | ||
source: SourceObjectType, | ||
mapper: mapObject.Mapper< | ||
SourceObjectType, | ||
MappedObjectKeyType, | ||
MappedObjectValueType | ||
>, | ||
options: mapObject.DeepOptions | ||
): {[key: string]: unknown}; | ||
declare function mapObject< | ||
SourceObjectType extends {[key: string]: unknown}, | ||
TargetObjectType extends {[key: string]: unknown}, | ||
MappedObjectKeyType extends string, | ||
MappedObjectValueType extends unknown | ||
>( | ||
source: SourceObjectType, | ||
mapper: mapObject.Mapper< | ||
SourceObjectType, | ||
MappedObjectKeyType, | ||
MappedObjectValueType | ||
>, | ||
options: mapObject.TargetOptions<TargetObjectType> | ||
): TargetObjectType & {[K in MappedObjectKeyType]: MappedObjectValueType}; | ||
declare function mapObject< | ||
SourceObjectType extends {[key: string]: unknown}, | ||
MappedObjectKeyType extends string, | ||
MappedObjectValueType extends unknown = unknown | ||
>( | ||
source: SourceObjectType, | ||
mapper: mapObject.Mapper< | ||
SourceObjectType, | ||
MappedObjectKeyType, | ||
MappedObjectValueType | ||
>, | ||
options?: mapObject.Options | ||
): {[K in MappedObjectKeyType]: MappedObjectValueType}; | ||
|
||
export = mapObject; |
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,34 @@ | ||
import {expectType} from 'tsd'; | ||
import mapObject = require('.'); | ||
|
||
const options: mapObject.Options = {}; | ||
|
||
const newObject = mapObject({foo: 'bar'}, (key, value) => [value, key]); | ||
expectType<{[x: string]: 'foo'}>(newObject); | ||
expectType<'foo'>(newObject.bar); | ||
|
||
const obj = mapObject({foo: 'bar'}, (key, value) => [value, key], { | ||
target: {baz: 'baz'} | ||
}); | ||
expectType<{baz: string} & {[x: string]: 'foo'}>(obj); | ||
expectType<'foo'>(obj.bar); | ||
expectType<string>(obj.baz); | ||
|
||
const obj1 = mapObject({foo: 'bar'}, (key, value) => [value, key], { | ||
target: {baz: 'baz'}, | ||
deep: false | ||
}); | ||
expectType<{baz: string} & {[x: string]: 'foo'}>(obj1); | ||
expectType<'foo'>(obj1.bar); | ||
expectType<string>(obj1.baz); | ||
|
||
const obj2 = mapObject({foo: 'bar'}, (key, value) => [value, key], { | ||
deep: true | ||
}); | ||
expectType<{[key: string]: unknown}>(obj2); | ||
const obj3 = mapObject({foo: 'bar'}, (key, value) => [value, key], { | ||
deep: true, | ||
target: {bar: 'baz' as const} | ||
}); | ||
expectType<{[key: string]: unknown}>(obj3); | ||
expectType<'baz'>(obj3.bar); |
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