1.1.0 • Published 5 months ago

@idapgroup/js-object-utils v1.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

Js object utils NPM Module

Javascript functions for transform or mutate object

Installation

npm install @idapgroup/js-object-utils

or

yarn add @idapgroup/js-object-utils

Usage

isObject

Detect is object created by the Object constructor

import { isObject } from '@idapgroup/js-object-utils';

const object = {
  /**
   * key: value
   * values can be primitives or objects
   */
};

const isObj = isObject(object) // true of false;

createFormData

Serialize object to FormData instance.

DEMO

const options = {
  /**
   * prefix for form data key
   * defaults empty string
   */
  keyPrefix: '',

  /**
   * prefix index for form data key
   * defaults to null
   */
  index: null,

  /**
   * callback fn for convert true or false
   * defaults true or false to '1' or '0' respectively
   */
  booleanMapper: (val: boolean) => (val ? '1' : '0'),

  /**
   * allow empty string or array
   * defaults to false
   */
  allowEmptyValues: false,
  
  /**
   * allow nullable values, ignore if emptyValues not allow 
   * defaults to false
   */
  allowNullableValues: false,
};
import { createFormData } from '@idapgroup/js-object-utils';

const object = {
  /**
   * key: value
   * values can be primitives or objects
   */
};

const formData = createFormData(
  object,
  options, // optional
  existingFormData, // optional
);

createURLParams

Serialize object to object with key and primitive value.

DEMO

const options = {
  /**
   * prefix for form data key
   * defaults empty string
   */
  keyPrefix: '',

  /**
   * prefix index for form data key
   * defaults to null
   */
  index: null,

  /**
   * callback fn for convert true or false
   * defaults true or false to '1' or '0' respectively
   */
  booleanMapper: (val: boolean) => (val ? '1' : '0'),
};
import { createURLParams } from '@idapgroup/js-object-utils';

const object = {
  /**
   * key: value
   * values can be primitives or objects
   */
};

const params = createURLParams(
  object,
  options, // optional
);

cleanObject

Remove nullable/empty values in object

DEMO

const options = {
  /**
   * remove empty string, object, array in object
   * defaults false
   */
  removeEmptyValues: false,
};
import { cleanObject } from '@idapgroup/js-object-utils';

const object = {
  /**
   * key: value
   * values can be primitives or objects
   */
};

const obj = cleanObject(
  object, 
  options, // optional
);

keysToSnakeCase

Transform object keys to snake case

import { cleanObject } from '@idapgroup/keysToSnakeCase';

const object = {
  /**
   * key: value
   * values can be primitives or objects
   */
};

const obj = keysToSnakeCase(
  object
);
1.1.0

5 months ago

1.0.1

11 months ago

1.0.0

1 year ago

0.2.1

1 year ago

0.2.0

1 year ago

0.1.9

1 year ago

0.1.8

1 year ago

0.1.7

1 year ago

0.1.6

1 year ago

0.1.5

1 year ago

0.1.4

1 year ago

0.1.3

1 year ago

0.1.2

1 year ago

0.1.1-b

1 year ago

0.1.1-a

1 year ago

0.1.1

1 year ago

0.1.0

1 year ago