map-obj

  • Version 5.0.2
  • Published
  • 9.4 kB
  • No dependencies
  • MIT license

Install

npm i map-obj
yarn add map-obj
pnpm add map-obj

Overview

Map object keys and values into a new object

Index

Variables

variable mapObjectSkip

const mapObjectSkip: Symbol;
  • Return this value from a mapper function to remove a key from an object.

    Example 1

    import mapObject, {mapObjectSkip} from 'map-obj';
    const object = {one: 1, two: 2}
    const mapper = (key, value) => value === 1 ? [key, value] : mapObjectSkip
    const result = mapObject(object, mapper);
    console.log(result);
    //=> {one: 1}

Functions

function mapObject

mapObject: {
<
SourceObjectType extends Record<string, unknown>,
TargetObjectType extends Record<string, any>,
MappedObjectKeyType extends string,
MappedObjectValueType
>(
source: SourceObjectType,
mapper: Mapper<SourceObjectType, MappedObjectKeyType, MappedObjectValueType>,
options: DeepOptions & TargetOptions<TargetObjectType>
): TargetObjectType & Record<string, unknown>;
<
SourceObjectType extends Record<string, unknown>,
MappedObjectKeyType extends string,
MappedObjectValueType
>(
source: SourceObjectType,
mapper: Mapper<SourceObjectType, MappedObjectKeyType, MappedObjectValueType>,
options: DeepOptions
): Record<string, unknown>;
<
SourceObjectType extends Record<string, any>,
TargetObjectType extends Record<string, any>,
MappedObjectKeyType extends string,
MappedObjectValueType
>(
source: SourceObjectType,
mapper: Mapper<SourceObjectType, MappedObjectKeyType, MappedObjectValueType>,
options: TargetOptions<TargetObjectType>
): TargetObjectType & { [K in MappedObjectKeyType]: MappedObjectValueType };
<
SourceObjectType extends Record<string, any>,
MappedObjectKeyType extends string,
MappedObjectValueType
>(
source: SourceObjectType,
mapper: Mapper<SourceObjectType, MappedObjectKeyType, MappedObjectValueType>,
options?: Options
): { [K in MappedObjectKeyType]: MappedObjectValueType };
};
  • Map object keys and values into a new object.

    Parameter source

    The source object to copy properties from.

    Parameter mapper

    A mapping function.

    Example 1

    import mapObject, {mapObjectSkip} from 'map-obj';
    const newObject = mapObject({foo: 'bar'}, (key, value) => [value, key]);
    //=> {bar: 'foo'}
    const newObject = mapObject({FOO: true, bAr: {bAz: true}}, (key, value) => [key.toLowerCase(), value]);
    //=> {foo: true, bar: {bAz: true}}
    const newObject = mapObject({FOO: true, bAr: {bAz: true}}, (key, value) => [key.toLowerCase(), value], {deep: true});
    //=> {foo: true, bar: {baz: true}}
    const newObject = mapObject({one: 1, two: 2}, (key, value) => value === 1 ? [key, value] : mapObjectSkip);
    //=> {one: 1}

Interfaces

interface DeepOptions

interface DeepOptions extends Options {}

    property deep

    readonly deep: true;

      interface MapperOptions

      interface MapperOptions {}

        property shouldRecurse

        readonly shouldRecurse?: boolean;
        • Whether targetValue should be recursed.

          Requires deep: true.

          true

        interface Options

        interface Options {}

          property deep

          readonly deep?: boolean;
          • Recurse nested objects and objects in arrays.

            false

          property target

          readonly target?: Record<string, any>;
          • The target object to map properties on to.

            {}

          interface TargetOptions

          interface TargetOptions<TargetObjectType extends Record<string, any>>
          extends Options {}

            property target

            readonly target: TargetObjectType;

              Type Aliases

              type Mapper

              type Mapper<
              SourceObjectType extends Record<string, any>,
              MappedObjectKeyType extends string,
              MappedObjectValueType
              > = (
              sourceKey: keyof SourceObjectType,
              sourceValue: SourceObjectType[keyof SourceObjectType],
              source: SourceObjectType
              ) =>
              | [
              targetKey: MappedObjectKeyType,
              targetValue: MappedObjectValueType,
              mapperOptions?: MapperOptions
              ]
              | typeof mapObjectSkip;

                Package Files (1)

                Dependencies (0)

                No dependencies.

                Dev Dependencies (3)

                Peer Dependencies (0)

                No peer dependencies.

                Badge

                To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

                You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/map-obj.

                • Markdown
                  [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/map-obj)
                • HTML
                  <a href="https://www.jsdocs.io/package/map-obj"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>