Skip to content

Commit

Permalink
Adding new utils
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderkalachev committed Dec 4, 2024
1 parent 4825cc3 commit 290c7d6
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 3 deletions.
6 changes: 6 additions & 0 deletions packages/@weresk/core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @weresk/core

## 0.2.10

### Patch Changes

- Adding new utils

## 0.2.9

### Patch Changes
Expand Down
4 changes: 4 additions & 0 deletions packages/@weresk/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ Common utilities and types for the rest of @weresk libraries

## Utils

- **Arrays**
- **`pickRandom<T>(array?: T[]): T | undefined`**: Returns a random element from an array
- **`shuffle<T>(array: T[]): T[]`**: Shuffles an array
- **Objects**
- **`isObject(item?: any): boolean`**: Checks if a variable is an object and not an array
- **`mapKeys<K extends string, T>(keys: K[], fn: (arg: K) => T): Record<K, T>`**: Creates an object from an array of keys, using map function
Expand All @@ -33,6 +36,7 @@ Common utilities and types for the rest of @weresk libraries
- **`purgeEmptyStrings<T extends object>(obj: T): Partial<T>`**: Converts empty strings in the object values to `undefined`
- **Styles**
- **`colorToRGB(color: Color): string`**: Converts Sanity Color object to `R G B` string
- **`lockBodyScroll(lock = true)`**: Locks and unlocks scroll for HTML body element
- **Text**
- **`caseTransform(input: string | undefined, transform: "capitalize" | "uppercase" | "lowercase" | "title"): string`**: Transforms a string case
- **`capitalize(input: string | undefined): string`**: Capitalizes a string
Expand Down
2 changes: 1 addition & 1 deletion packages/@weresk/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@weresk/core",
"version": "0.2.9",
"version": "0.2.10",
"private": false,
"main": "dist/index.js",
"module": "dist/index.mjs",
Expand Down
1 change: 1 addition & 0 deletions packages/@weresk/core/src/types/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface DefaultStyleProps {

export interface DefaultProps extends DefaultStyleProps {
id?: string;
title?: string;
}

export interface DefaultPropsWithChildren extends DefaultProps {
Expand Down
2 changes: 2 additions & 0 deletions packages/@weresk/core/src/utils/arrays/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./pickRandom";
export * from "./shuffle";
4 changes: 4 additions & 0 deletions packages/@weresk/core/src/utils/arrays/pickRandom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export function pickRandom<T>(array?: T[]): T | undefined {
if (!array?.length) return undefined;
return array[Math.floor(Math.random() * array.length)];
}
16 changes: 16 additions & 0 deletions packages/@weresk/core/src/utils/arrays/shuffle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export function shuffle<T>(array: T[]): T[] {
let currentIndex = array.length;
let randomIndex: number;

// While there remain elements to shuffle.
while (currentIndex > 0) {
// Pick a remaining element.
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex--;

// And swap it with the current element.
[array[currentIndex], array[randomIndex]] = [array[randomIndex] as T, array[currentIndex] as T];
}

return array;
}
3 changes: 2 additions & 1 deletion packages/@weresk/core/src/utils/styles/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { colorToRGB } from "./colorToRgb";
export * from "./colorToRgb";
export * from "./lockBodyScroll";
4 changes: 4 additions & 0 deletions packages/@weresk/core/src/utils/styles/lockBodyScroll.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export function lockBodyScroll(lock = true) {
const body = document.querySelector("body");
body?.style.setProperty("overflow", lock ? "hidden" : "auto");
}
8 changes: 8 additions & 0 deletions packages/@weresk/maket/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# @weresk/maket

## 0.2.19

### Patch Changes

- Updated dependencies
- @weresk/core@0.2.10
- @weresk/locales@0.0.12

## 0.2.18

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/@weresk/maket/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@weresk/maket",
"version": "0.2.18",
"version": "0.2.19",
"private": false,
"exports": {
".": {
Expand Down

0 comments on commit 290c7d6

Please sign in to comment.