Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@gmana/utils

Package Overview
Dependencies
Maintainers
1
Versions
94
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@gmana/utils - npm Package Compare versions

Comparing version 0.0.7 to 0.0.8

./dist/index.js

82

dist/index.d.ts

@@ -1,22 +0,80 @@

import * as React from 'react';
import { ClassValue } from 'clsx';
declare function cn(...inputs: ClassValue[]): string;
declare function colorLuminance(hex: string, lum: number): string;
declare const convertHexToRGB: (hex: string) => string | undefined;
declare function darken(col: string, amt: number): string;
declare const flatten: (o: Record<string, any>) => Record<string, any>;
declare function getColor(key: string): string;
declare function hexToHsl(hex: string): [number, number, number];
declare function hexToRgb(hex: string): [number, number, number];
declare function hexToRgbA(hex: string, opacity: number): string;
declare function lighten(col: string, amt: number): string;
declare function shuffle<T>(a: T[]): void;
declare function toRGB(hex: string, alpha: number): string;
/**
* Return a slugified copy of a string.
*
* @param {string} str The string to be slugified
* @return {string} The slugified string.
* @description
* remove: `''` or `undefined` or `null`
*
*/
declare function toSlug(str: string): string;
declare function compactOject(o: any): Partial<any>;
declare function formatDate(input: string | number | Date): string;
declare function fetcher<JSON = never>(input: RequestInfo, init?: RequestInit): Promise<JSON>;
/**
* On the server, React emits a warning when calling `useLayoutEffect`.
* This is because neither `useLayoutEffect` nor `useEffect` run on the server.
* We use this safe version which suppresses the warning by replacing it with a noop on the server.
*
* See: https://reactjs.org/docs/hooks-reference.html#uselayouteffect
* @returns `_cu2mgj0`
*/
declare const useIsomorphicLayoutEffect: typeof React.useLayoutEffect;
declare function v1(): string;
/**
*
* @returns `G1645442268600931`
*/
declare function v2(): string;
/**
*
* @returns `e76f10ac-3c1c-8941-6ea2-28701e8adfd5`
*/
declare function v3(): string;
declare function usePrevious<T>(value: T): T;
declare const getImageInContent: (content: string) => string;
export { toSlug, useIsomorphicLayoutEffect, usePrevious };
declare const isBrowser: boolean;
declare const isNavigator: boolean;
declare const getRandomInt: (min?: number, max?: number) => number;
declare const getRandomInitials: () => string;
declare const slugify: (...args: (string | number)[]) => string;
declare const htmlToText: (html: string, limit?: number) => string;
/**
* @description
* Convert numbers or strings to pixel value,
* Helpful for styled-jsx when using a prop
* @example
* ```tsx
* height: ${toPixels(height)}; (supports height={20} and height="20px")
* ```
*/
declare const toPixel: (value: string | number) => string;
export { cn, colorLuminance, compactOject, convertHexToRGB, darken, fetcher, flatten, formatDate, getColor, getImageInContent, getRandomInitials, getRandomInt, hexToHsl, hexToRgb, hexToRgbA, htmlToText, isBrowser, isNavigator, lighten, shuffle, slugify, toPixel, toRGB, v1, v2, v3 };
"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;

@@ -20,7 +18,23 @@ var __export = (target, all) => {

};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var __async = (__this, __arguments, generator) => {
return new Promise((resolve, reject) => {
var fulfilled = (value) => {
try {
step(generator.next(value));
} catch (e) {
reject(e);
}
};
var rejected = (value) => {
try {
step(generator.throw(value));
} catch (e) {
reject(e);
}
};
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
step((generator = generator.apply(__this, __arguments)).next());
});
};

@@ -30,41 +44,429 @@ // src/index.tsx

__export(src_exports, {
toSlug: () => toSlug,
useIsomorphicLayoutEffect: () => useIsomorphicLayoutEffect,
usePrevious: () => usePrevious
cn: () => cn,
colorLuminance: () => colorLuminance,
compactOject: () => compactOject,
convertHexToRGB: () => convertHexToRGB,
darken: () => darken,
fetcher: () => fetcher,
flatten: () => flatten,
formatDate: () => formatDate,
getColor: () => getColor,
getImageInContent: () => getImageInContent,
getRandomInitials: () => getRandomInitials,
getRandomInt: () => getRandomInt,
hexToHsl: () => hexToHsl,
hexToRgb: () => hexToRgb,
hexToRgbA: () => hexToRgbA,
htmlToText: () => htmlToText,
isBrowser: () => isBrowser,
isNavigator: () => isNavigator,
lighten: () => lighten,
shuffle: () => shuffle,
slugify: () => slugify,
toPixel: () => toPixel,
toRGB: () => toRGB,
v1: () => v1,
v2: () => v2,
v3: () => v3
});
module.exports = __toCommonJS(src_exports);
// src/toSlug.ts
function toSlug(str) {
let s = str;
if (!s) {
return "";
// src/lib/cn/cn.ts
var import_clsx = require("clsx");
var import_tailwind_merge = require("tailwind-merge");
function cn(...inputs) {
return (0, import_tailwind_merge.twMerge)((0, import_clsx.clsx)(inputs));
}
// src/lib/color/color-luminance.ts
function colorLuminance(hex, lum) {
hex = String(hex).replace(/[^0-9a-f]/gi, "");
if (hex.length < 6) {
hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2];
}
s = s.toLowerCase().trim();
s = s.replace(/ & /g, " and ");
s = s.replace(/[ ]+/g, "-");
s = s.replace(/[-]+/g, "-");
s = s.replace(/[^a-z0-9-]+/g, "");
return s;
lum = lum || 0;
let rgb = "#", c, i;
for (i = 0; i < 3; i++) {
c = parseInt(hex.substring(i * 2, 2), 16);
c = Math.round(Math.min(Math.max(0, c + c * lum), 255)).toString(16);
rgb += ("00" + c).substring(c.length);
}
return rgb;
}
// src/useIsomorphicLayoutEffect.tsx
var React = __toESM(require("react"));
var useIsomorphicLayoutEffect = typeof window !== "undefined" ? React.useLayoutEffect : () => {
// src/lib/color/convert-hex-to-rgb.ts
var convertHexToRGB = (hex) => {
if (hex.match("rgba")) {
const triplet = hex.slice(5).split(",").slice(0, -1).join(",");
return triplet;
}
let c;
if (/^#([A-Fa-f0-9]{3}){1,2}$/.test(hex)) {
c = hex.substring(1).split("");
if (c.length === 3) {
c = [c[0], c[0], c[1], c[1], c[2], c[2]];
}
c = "0x" + c.join("");
return [c >> 16 & 255, c >> 8 & 255, c & 255].join(",");
}
return void 0;
};
// src/usePrevious.tsx
var React2 = __toESM(require("react"));
function usePrevious(value) {
const ref = React2.useRef(value);
React2.useEffect(() => {
ref.current = value;
}, [value]);
return ref.current;
// src/lib/color/darken.ts
function darken(col, amt) {
amt = Math.abs(amt);
amt = amt / 100 * -1;
return colorLuminance(col, amt);
}
// src/lib/color/flatten.ts
var flatten = (o) => {
return Object.assign(
{},
...function _flatten(objectBit, path = "") {
return [].concat(
...Object.keys(objectBit).map(
//iterate over object
(key) => typeof objectBit[key] === "object" ? _flatten(objectBit[key], `${path}-${key}`.replace(/^-/, "")) : { [`${path}-${key}`.replace(/^-/, "")]: objectBit[key] }
//append object with it’s path as key
)
);
}(o)
);
};
// src/lib/color/get-color.ts
function getColor(key) {
const colors = {
transparent: "transparent",
black: "#000",
white: "#fff",
gray: {
"50": "#f9fafb",
"100": "#f3f4f6",
"200": "#e5e7eb",
"300": "#d1d5db",
"400": "#9ca3af",
"500": "#6b7280",
"600": "#4b5563",
"700": "#374151",
"800": "#1f2937",
"900": "#111827"
},
red: {
"50": "#fef2f2",
"100": "#fee2e2",
"200": "#fecaca",
"300": "#fca5a5",
"400": "#f87171",
"500": "#ef4444",
"600": "#dc2626",
"700": "#b91c1c",
"800": "#991b1b",
"900": "#7f1d1d"
},
yellow: {
"50": "#fffbeb",
"100": "#fef3c7",
"200": "#fde68a",
"300": "#fcd34d",
"400": "#fbbf24",
"500": "#f59e0b",
"600": "#d97706",
"700": "#b45309",
"800": "#92400e",
"900": "#78350f"
},
green: {
"50": "#ecfdf5",
"100": "#d1fae5",
"200": "#a7f3d0",
"300": "#6ee7b7",
"400": "#34d399",
"500": "#10b981",
"600": "#059669",
"700": "#047857",
"800": "#065f46",
"900": "#064e3b"
},
blue: {
"50": "#eff6ff",
"100": "#dbeafe",
"200": "#bfdbfe",
"300": "#93c5fd",
"400": "#60a5fa",
"500": "#3b82f6",
"600": "#2563eb",
"700": "#1d4ed8",
"800": "#1e40af",
"900": "#1e3a8a"
},
indigo: {
"50": "#eef2ff",
"100": "#e0e7ff",
"200": "#c7d2fe",
"300": "#a5b4fc",
"400": "#818cf8",
"500": "#6366f1",
"600": "#4f46e5",
"700": "#4338ca",
"800": "#3730a3",
"900": "#312e81"
},
purple: {
"50": "#f5f3ff",
"100": "#ede9fe",
"200": "#ddd6fe",
"300": "#c4b5fd",
"400": "#a78bfa",
"500": "#8b5cf6",
"600": "#7c3aed",
"700": "#6d28d9",
"800": "#5b21b6",
"900": "#4c1d95"
},
pink: {
"50": "#fdf2f8",
"100": "#fce7f3",
"200": "#fbcfe8",
"300": "#f9a8d4",
"400": "#f472b6",
"500": "#ec4899",
"600": "#db2777",
"700": "#be185d",
"800": "#9d174d",
"900": "#831843"
}
};
const flattenedColors = flatten(colors);
return flattenedColors[key];
}
// src/lib/color/hex-to-hsl.ts
function hexToHsl(hex) {
if (hex) {
const sanitizedHex = hex.replace("#", "");
const red = parseInt(sanitizedHex.substring(0, 2), 16);
const green = parseInt(sanitizedHex.substring(2, 4), 16);
const blue = parseInt(sanitizedHex.substring(4, 6), 16);
const normalizedRed = red / 255;
const normalizedGreen = green / 255;
const normalizedBlue = blue / 255;
const max = Math.max(normalizedRed, normalizedGreen, normalizedBlue);
const min = Math.min(normalizedRed, normalizedGreen, normalizedBlue);
let hue, saturation, lightness;
if (max === min) {
hue = 0;
} else if (max === normalizedRed) {
hue = (normalizedGreen - normalizedBlue) / (max - min) % 6;
} else if (max === normalizedGreen) {
hue = (normalizedBlue - normalizedRed) / (max - min) + 2;
} else {
hue = (normalizedRed - normalizedGreen) / (max - min) + 4;
}
hue = Math.round(hue * 60);
if (hue < 0) {
hue += 360;
}
lightness = (max + min) / 2;
if (max === min) {
saturation = 0;
} else if (lightness <= 0.5) {
saturation = (max - min) / (max + min);
} else {
saturation = (max - min) / (2 - max - min);
}
saturation = Math.round(saturation * 100);
lightness = Math.round(lightness * 100);
return [hue, saturation, lightness];
}
return [0, 0, 0];
}
// src/lib/color/hex-to-rgb.ts
function hexToRgb(hex) {
if (hex) {
const sanitizedHex = hex.replace("#", "");
const red = parseInt(sanitizedHex.substring(0, 2), 16);
const green = parseInt(sanitizedHex.substring(2, 4), 16);
const blue = parseInt(sanitizedHex.substring(4, 6), 16);
return [red, green, blue];
}
return [0, 0, 0];
}
// src/lib/color/hex-to-rgba.ts
function hexToRgbA(hex, opacity) {
let c;
const o = opacity || 1;
if (/^#([A-Fa-f0-9]{3}){1,2}$/.test(hex)) {
c = hex.substring(1).split("");
if (c.length === 3) {
c = [c[0], c[0], c[1], c[1], c[2], c[2]];
}
c = "0x" + c.join("");
return "rgba(" + [c >> 16 & 255, c >> 8 & 255, c & 255].join(",") + "," + o + ")";
}
return "";
}
// src/lib/color/lighten.ts
function lighten(col, amt) {
amt = Math.abs(amt) / 100;
return colorLuminance(col, amt);
}
// src/lib/color/shuffle.ts
function shuffle(a) {
for (let i = a.length; i; i--) {
const j = Math.floor(Math.random() * i);
[a[i - 1], a[j]] = [a[j], a[i - 1]];
}
}
// src/lib/color/to-rgb.ts
function toRGB(hex, alpha) {
const r = parseInt(hex.slice(1, 3), 16), g = parseInt(hex.slice(3, 5), 16), b = parseInt(hex.slice(5, 7), 16);
if (alpha) {
return `rgba(${r}, ${g}, ${b}, ${alpha})`;
} else {
return `rgb(${r}, ${g}, ${b})`;
}
}
// src/lib/compact-object/compact-object.ts
function compactOject(o) {
if (Array.isArray(o))
return o;
const obj = {};
Object.keys(o).forEach((key) => {
if (o[key] && typeof o[key] === "object") {
const nestedObj = compactOject(o[key]);
if (Object.keys(nestedObj).length) {
obj[key] = nestedObj;
}
} else if (o[key] !== "" && o[key] !== void 0 && o[key] !== null) {
obj[key] = o[key];
}
});
return obj;
}
// src/lib/date/format-date.ts
function formatDate(input) {
const date = new Date(input);
return date.toLocaleDateString("en-US", {
month: "long",
day: "numeric",
year: "numeric"
});
}
// src/lib/fetcher/fetcher.ts
function fetcher(input, init) {
return __async(this, null, function* () {
const res = yield fetch(input, init);
return res.json();
});
}
// src/lib/id/id.ts
function v1() {
return "_" + Math.random().toString(36).substring(2, 9);
}
function v2() {
const tempId = Math.random().toString();
const uid = `G${tempId.substring(2, tempId.length - 1)}`;
return uid;
}
function v3() {
const hashTable = ["a", "b", "c", "d", "e", "f", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"];
const uuid = [];
for (let i = 0; i < 36; i++) {
if (i === 8 || i === 13 || i === 18 || i === 23) {
uuid[i] = "-";
} else {
uuid[i] = hashTable[Math.ceil(Math.random() * hashTable.length - 1)];
}
}
return uuid.join("");
}
// src/lib/image/get-image-in-content.ts
var getImageInContent = (content) => {
var _a;
const images = (_a = content.match("http[^\\s]+(jpg|jpeg|png|tiff|gif)\\b")) != null ? _a : "";
return images[0];
};
// src/lib/is/is-browser.ts
var isBrowser = typeof window !== "undefined";
// src/lib/is/is-navigator.ts
var isNavigator = typeof navigator !== "undefined";
// src/lib/random/get-random-int.ts
var getRandomInt = (min = 0, max = 100) => {
const _min = Math.ceil(min);
const _max = Math.floor(max);
return Math.floor(Math.random() * (_max - _min + 1)) + _min;
};
// src/lib/random/get-random-text.ts
var getRandomInitials = () => {
return Math.random().toString(36).replace(/[^a-z]+/g, "").substring(0, 2).toUpperCase();
};
// src/lib/slugify/slugify.ts
var slugify = (...args) => {
const value = args.join(" ");
return value.normalize("NFD").replace(/[\u0300-\u036f]/g, "").toLowerCase().trim().replace(/[^a-z0-9 ]/g, "").replace(/\s+/g, "-");
};
// src/lib/text/html-to-text.ts
var htmlToText = (html, limit = 68) => {
if (html.length < 1)
return "";
let text = html.replace(/<[^>]+>/g, "");
if (text.length >= limit) {
text = text.substring(0, limit) + "...";
}
return text;
};
// src/lib/to-pixel/to-pixel.ts
var toPixel = (value) => {
if (typeof value === "number") {
return `${value}px`;
}
return value;
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
toSlug,
useIsomorphicLayoutEffect,
usePrevious
cn,
colorLuminance,
compactOject,
convertHexToRGB,
darken,
fetcher,
flatten,
formatDate,
getColor,
getImageInContent,
getRandomInitials,
getRandomInt,
hexToHsl,
hexToRgb,
hexToRgbA,
htmlToText,
isBrowser,
isNavigator,
lighten,
shuffle,
slugify,
toPixel,
toRGB,
v1,
v2,
v3
});

25

package.json
{
"name": "@gmana/utils",
"version": "0.0.7",
"version": "0.0.8",
"main": "./dist/index.js",

@@ -13,11 +13,17 @@ "module": "./dist/index.mjs",

"devDependencies": {
"@types/react": "^18.2.13",
"@types/react-dom": "^18.2.6",
"clsx": "^1.2.1",
"eslint": "^8.43.0",
"react": "^18.2.0",
"tailwind-merge": "^1.13.2",
"tsup": "^7.0.0",
"typescript": "^5.1.3",
"@gmana/tsconfig": "0.0.2",
"@types/react": "^18.2.0",
"@types/react-dom": "^18.2.0",
"eslint": "^7.32.0",
"eslint-config-gmana": "0.0.2",
"react": "^18.2.0",
"tsup": "^5.10.1",
"typescript": "^4.5.3"
"eslint-config-gmana": "0.0.3"
},
"peerDependencies": {
"clsx": "^1.2.1",
"tailwind-merge": "^1.13.2"
},
"publishConfig": {

@@ -31,4 +37,5 @@ "access": "public"

"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist",
"version-packages": "changeset version"
"version-packages": "changeset version",
"test": "vitest"
}
}

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc