Skip to content

idapgroup/js-object-utils

Repository files navigation

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

Options
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

Options
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

Options
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 { keysToSnakeCase } from '@idapgroup/keysToSnakeCase';

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

const obj = keysToSnakeCase(object);