Javascript functions for transform or mutate object
npm install @idapgroup/js-object-utils
or
yarn add @idapgroup/js-object-utils
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;
Serialize object to FormData instance.
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
);
Serialize object to object with key and primitive value.
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
);
Remove nullable/empty values in object
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
);
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);