@leexi/shared
v0.7.4
Published
<p align="center"><img src="https://github.com/user-attachments/assets/581a8322-430f-4912-bc11-60fc0ca099f4" width="500"></p><br><br><br>
Readme
@leexi/shared is a collection of config files and utility functions for JavaScript or TypeScript based repositories as well as composables and components for Vue based repositores of the Leexi-ai organisation.
Installation
yarn add -D @leexi/sharedUsage
esLint
There are 2 available esLint configs.
// eslint.config.js
import base from '@leexi/shared/eslint/base';
export default [
...base,
{
// add custom config here
}
];Utils
import { deepDup, isSame, set } from '@leexi/shared/utils';
const foo = { foo: 'bar' };
const dup = deepDup(foo);
if (isSame(foo, dup)) {
set(foo, ['foo'], 'fiz');
}Composables
<script setup>
import { useLocalStorage } from '@leexi/shared/composables'
const mode = useLocalStorage('mode', 'dark')
</script>Nuxt
This package provides a module for Nuxt to auto import every component, composable and util in a Nuxt based repository
// nuxt.config.ts
export default defineNuxtConfig({
modules: [
'@leexi/shared'
],
leexi: {
composables: true, // whether composables should be auto-imported. Defaults to true
utils: true, // whether utils should be auto-imported. Defaults to true
}
})API
Composables
useLocalStorage
A composable that provides a reactive reference to a value stored in localStorage. It handles JSON parsing/stringifying and migration from kebab-case/snake_case keys to camelCase.
- Type
const useLocalStorage: <T>(key: string, defaultValue?: T) => Ref<undefined | T>;- Example
const userSetting = useLocalStorage('userSetting', { theme: 'light' });
console.log(userSetting.value); // Output: { theme: 'light' }
console.log(localStorage.userSetting); // Output: '{"theme":"light"}'
const sameUserSetting = useLocalStorage('userSetting');
console.log(sameUserSetting.value); // Output: { theme: 'light' }
userSetting.value = { theme: 'dark', notifications: true };
console.log(sameUserSetting.value); // Output: { theme: 'dark', notifications: true }
console.log(localStorage.userSetting); // Output: '{"theme":"dark","notifications":true}'Utils
Array
castArray
- Type
filterMap
Returns a new array containing the truthy results (everything except false, '', 0, undefined and null) of running the given callback for every item.
- Type
const filterMap: <T>(items: T[], callback: (_item: T) => unknown) => unknown[];- Example
const items = [0, 1];
const foos = filterMap(records, foo => foo2);
console.log(foos); // Output: [2]groupBy
- Type
joinBy
- Type
partition
- Type
pluck
Extract the given attribute from each element in the array.
- Type
const pluck: <T extends Record<string, unknown>, K extends keyof T>(records: T[], attr: K) => T[K][];- Example
const records = [{ foo: 1 }, { foo: 2 }];
const foos = pluck(records, 'foo');
console.log(foos); // Output: [1, 2]removeRecord
- Type
replaceRecord
- Type
sortBy
- Type
const sortBy: <T, C extends (_: T) => Primitive, K extends Extract<FilteredAttrs<T, Primitive>, string>>(items: T[], attrsOrCallbacks: C | K | `-${K}` | (C | K | `-${K}`)[]) => T[];sum
Returns the sum of elements.
- Type
const sum: (numbers: number[]) => number;- Example
const sum = sum([1, 2, 3])
console.log(sum); // Output: 6sumBy
Returns the sum of elements at a given attribute.
- Type
const sumBy: <T extends Record<string, unknown>>(records: T[], attr: FilteredAttrs<T, number>) => number;- Example
const records = [{ foo: 1 }, { foo: 2 }];
const sum = sumBy(records, 'foo');
console.log(sum); // Output: 3uniq
Returns a new array by removing duplicate values in self.
- Type
const uniq: (primitives: Primitive[]) => Primitive[];- Example
const uniq = uniq([true, true, false, 1, 1, 2, 'foo', 'foo', 'bar'])
console.log(uniq); // Output: [true, false, 1, 2, 'foo', 'bar']uniqBy
- Type
Function
debounce
- Type
Number
rand
Returns a random integer greater than or equal to min and less than or equal to max.
If the min is bigger than the max then NaN is returned.
- Type
const rand: (max: number, min?: number) => number;- Example
const randOne = rand(2);
console.log(randOne); // Output: 1
const randTwo = rand(2, -2);
console.log(randTwo); // Output: -1toPadStart
- Type
Objects
deepDup
Creates a deep duplicate of an object, array, or Set. It handles nested objects, arrays, and Sets but does not handle functions, Dates, or circular references.
- Type
const deepDup: <T extends object>(object: T) => T;- Example
const originalObject = { a: 1, b: { c: 2, d: [3, 4] }, e: new Set([5, 6]) };
const duplicatedObject = deepDup(originalObject);
console.log(duplicatedObject); // Output: { a: 1, b: { c: 2, d: [3, 4] }, e: Set { 5, 6 } }
console.log(originalObject === duplicatedObject); // Output: false
console.log(originalObject.b === duplicatedObject.b); // Output: false
console.log(originalObject.b.d === duplicatedObject.b.d); // Output: false
console.log(originalObject.e === duplicatedObject.e); // Output: falseisSame
Checks if two objects are the same by comparing their JSON stringified representations. Note: This method has limitations and may not accurately compare objects with different key orders, complex values like functions or Dates, or circular references.
- Type
const isSame: (a: object, b: object) => boolean;- Example
const obj1 = { a: 1, b: 2 };
const obj2 = { a: 1, b: 2 };
const obj3 = { b: 2, a: 1 };
console.log(isSame(obj1, obj2)); // Output: true
console.log(isSame(obj1, obj3)); // Output: false (due to key order difference in JSON stringification)Promise
singleFlight
- Type
sleep
- Type
throttle
- Type
Records
get
- Type
merge
- Type
omit
Creates a new object by omitting the specified keys from the input record.
- Type
const omit: <T extends Record<string, unknown>, K extends (keyof T)[]>(record: T, keys: K) => Omit<T, K[number]>;- Example
const myObject = { a: 1, b: 2, c: 3, d: 4 };
const omittedObject = omit(myObject, ['b', 'd']);
console.log(omittedObject); // Output: { a: 1, c: 3 }pick
Creates a new object by picking only the specified keys from the input record.
- Type
const pick: <T extends Record<string, unknown>, K extends (keyof T)[]>(record: T, keys: K) => Pick<T, K[number]>;- Example
const myObject = { a: 1, b: 2, c: 3, d: 4 };
const pickedObject = pick(myObject, ['a', 'c']);
console.log(pickedObject); // Output: { a: 1, c: 3 }set
Sets a value within a nested object structure based on a provided path. It mutates the original object and creates nested objects if they don't exist along the path.
- Type
const set: <T extends Record<string, unknown>, const P extends string[], V>(record: T, path: P, value: V) => SetRecord<T, P, V>;- Example
const myObject = { a: { b: { c: 1 } } };
set(myObject, ['a', 'b', 'd'], 2);
console.log(myObject); // Output: { a: { b: { c: 1, d: 2 } } }Strings
camelcase
Converts a string from snake_case or kebab-case to camelCase.
- Type
const camelcase: (string: string) => string;- Example
console.log(camelcase('my_variable_name')); // Output: myVariableName
console.log(camelcase('my-variable-name')); // Output: myVariableName
console.log(camelcase('myVariable')); // Output: myVariablecapitalize
Capitalizes the first letter of a string.
- Type
const capitalize: (string: string) => string;- Example
console.log(capitalize('hello world')); // Output: Hello world
console.log(capitalize('')); // Output: ''kebabcase
Converts a string from camelCase or snake_case to kebab-case.
- Type
const kebabcase: (string: string) => string;- Example
console.log(kebabcase('myVariableName')); // Output: my-variable-name
console.log(kebabcase('my_variable_name')); // Output: my-variable-name
console.log(kebabcase('my-variable-name')); // Output: my-variable-namesnakecase
Converts a string from camelCase or kebab-case to snake_case.
- Type
const snakecase: (string: string) => string;- Example
console.log(snakecase('myVariableName')); // Output: my_variable_name
console.log(snakecase('my-variable-name')); // Output: my_variable_name
console.log(snakecase('my_variable_name')); // Output: my_variable_namestrContains
- Type
truncate
- Type
urlify
- Type