@leexi/shared v0.3.7
@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
Records
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 } } }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 }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_nameObjects
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, non-primitive 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)8 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
11 months ago
11 months ago
11 months ago
11 months ago