@vue-formily/util

Common JavaScript utility library with performance, lightweight.

Usage no npm install needed!

<script type="module">
  import vueFormilyUtil from 'https://cdn.skypack.dev/@vue-formily/util';
</script>

README

Vue Formily Util

Common JavaScript utility library with performance, lightweight.

Getting Started

Installation

# install with yarn
yarn add @vue-formily/util

# install with npm
npm install @vue-formily/util --save

Utilities

isNullOrUndefined

Returns true if value is null or undefined.

isNullOrUndefined(value: any): boolean;

isPlainObject

Strict object type check. Only returns true for plain JavaScript objects.

isPlainObject(value: any): boolean;

isNumber

Returns true if value has type number.

isNumber(value: any): boolean;

isString

Returns true if value has type string.

isString(value: any): boolean;

isNumeric

Check if the input string contains only numeric characters.

isNumeric(value: string): boolean;

// Examples:
isNumeric('123'); // true
isNumeric('123.12'); // true
isNumeric('.12'); // true
isNumeric('-.12'); // true
isNumeric('-12.12'); // true
isNumeric('12e12'); // true
isNumeric(NaN); // false
isNumeric(null); // false
isNumeric([]); // false
isNumeric({}); // false
isNumeric(undefined); // false

isFunction

Returns true if value is a function.

isFunction(value: string): boolean;

isEmpty

Returns true if the object is empty. Empty is defined as:

  • null
  • undefined
  • a string with zero length
  • an array with no elements
  • a collection with no elements
isEmpty(value: any): boolean;

// Examples:
isEmpty(null); // true
isEmpty(undefined); // true
isEmpty(false); // false
isEmpty([]); // true
isEmpty({}); // true
isEmpty({ a: 1 }); // false
isEmpty([1]); // false
isEmpty(''); // true
isEmpty(' '); // false

merge

Merge objects or arrays

merge(target: any, ...sources: any[]): any;

// Examples:
merge({}, { test: 1, test2: [{ a: 1 }, 1] }, { test2: [{ b: 1 }] }); // { test: 1, test2: [{ a: 1, b: 1 }, 1] }
merge([1, { a: 1}], [2, { a: 2, b: 1 }, 3]); // [2, { a: 2, b: 1 }, 3]

get

Find the first found value in objects or arrays by its path.

get(path: string | string[], ...args: any[]): any;

// Examples:
get('a.b[1]', { a: { b: [1, 3] } }); // 3
get('[1]', [1], [2, 3]); // 3

findIndex

Returns the index of the first element in the array that satisfies the provided testing function, ortherwise, returns -1.

findIndex(arr: any[] = [], fn: (...args: any[]) => boolean): number;

// Examples:
findIndex([1, 2], n => n === 2); // 1
findIndex([null, { a: 1 }], n => n && n.a === 1); // 3

flatArray

Creates a new array with all sub-array elements concatenated into it recursively up to the specified depth.

flatArray(arr: any[], deep = Infinity): any[];

// Examples:
findIndex([1, 2], n => n === 2); // 1
findIndex([null, { a: 1 }], n => n && n.a === 1); // 3

Contributing

You are welcome to contribute to this project.

License

MIT