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

@byjohann/utils

Package Overview
Dependencies
Maintainers
0
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@byjohann/utils - npm Package Compare versions

Comparing version 0.3.1 to 0.3.2

3

dist/array.d.ts

@@ -0,1 +1,4 @@

/**
* Represents a value that can be either a single value or an array of values.
*/
type MaybeArray<T> = T | T[];

@@ -2,0 +5,0 @@ /**

@@ -8,4 +8,10 @@ /**

delimiter?: string): string;
/**
* Escapes a value for a CSV string.
*
* @remarks
* Returns an empty string if the value is `null` or `undefined`.
*/
declare function escapeCsvValue(value: unknown): string;
export { createCsv, escapeCsvValue };

@@ -8,2 +8,5 @@ /**

* Clones the given JSON value.
*
* @remarks
* The value must not contain circular references as JSON does not support them. It also must contain JSON serializable values.
*/

@@ -10,0 +13,0 @@ declare function cloneJSON<T>(value: T): T;

@@ -0,1 +1,7 @@

/**
* Interop helper for default exports.
*
* @example
* const mod = await interopDefault(import('./module.js'))
*/
declare function interopDefault<T>(m: T | Promise<T>): Promise<T extends {

@@ -2,0 +8,0 @@ default: infer U;

2

dist/object.d.ts

@@ -10,3 +10,3 @@ /**

/**
* Deeply applies a callback to each key-value pair in the object.
* Deeply applies a callback to every key-value pair in the given object, as well as nested objects and arrays.
*/

@@ -13,0 +13,0 @@ declare function deepApply<T extends Record<any, any>>(data: T, callback: (item: T, key: keyof T, value: T[keyof T]) => void): void;

@@ -0,8 +1,26 @@

/**
* Adds a leading slash to the given path if it does not already have one.
*/
declare function withLeadingSlash(path?: string): string;
/**
* Removes the trailing slash from the given path if it has one.
*/
declare function withoutTrailingSlash(path?: string): string;
/**
* Adds a trailing slash to the given path if it does not already have one
*/
declare function withTrailingSlash(path?: string): string;
/**
* Joins the given base URL and path, ensuring that there is only one slash between them.
*/
declare function joinURL(base?: string, path?: string): string;
/**
* Removes the base path from the input path, if it is present.
*/
declare function withoutBase(input?: string, base?: string): string;
/**
* Returns the pathname of the given path, which is the path without the query string.
*/
declare function getPathname(path?: string): string;
export { getPathname, joinURL, withLeadingSlash, withTrailingSlash, withoutBase, withoutTrailingSlash };
/**
* Simple template engine to replace variables in a string.
*
* @example
* const str = 'Hello, {name}!'
* const variables = { name: 'world' }
*
* console.log(template(str, variables)) // Hello, world!
*/

@@ -4,0 +10,0 @@ declare function template(str: string, variables: Record<string | number, any>, fallback?: string | ((key: string) => string)): string;

{
"name": "@byjohann/utils",
"type": "module",
"version": "0.3.1",
"version": "0.3.2",
"packageManager": "pnpm@9.5.0",

@@ -6,0 +6,0 @@ "description": "A collection of utilities for my projects",

@@ -20,4 +20,198 @@ # @byjohann/utils

## API
### Array
#### `toArray`
Converts `MaybeArray<T>` to `Array<T>`.
```ts
type MaybeArray<T> = T | T[]
declare function toArray<T>(array?: MaybeArray<T> | null | undefined): T[]
```
### CSV
#### `createCsv`
Converts an array of objects to a comma-separated values (CSV) string that contains only the `columns` specified.
```ts
declare function createCsv<T extends Record<string, any>>(
data: T[],
columns: (keyof T)[],
/** @default ',' */
delimiter?: string
): string
```
#### `escapeCsvValue`
Escapes a value for a CSV string.
> [!NOTE]
> Returns an empty string if the value is `null` or `undefined`.
```ts
declare function escapeCsvValue(value: unknown): string
```
### JSON
#### `tryParseJSON`
Type-safe wrapper around `JSON.stringify` falling back to the original value if it is not a string or an error is thrown.
```ts
declare function tryParseJSON<T = unknown>(value: unknown): T
```
#### `cloneJSON`
Clones the given JSON value.
> [!NOTE]
> The value must not contain circular references as JSON does not support them. It also must contain JSON serializable values.
```ts
declare function cloneJSON<T>(value: T): T
```
### Module
#### `interopDefault`
Interop helper for default exports.
```ts
declare function interopDefault<T>(m: T | Promise<T>): Promise<T extends {
default: infer U
} ? U : T>
```
**Example:**
```ts
import { interopDefault } from '@byjohann/utils'
const mod = await interopDefault(import('./module.js'))
```
### Object
#### `objectKeys`
Strictly typed `Object.keys`.
```ts
declare function objectKeys<T extends Record<any, any>>(obj: T): Array<`${keyof T & (string | number | boolean | null | undefined)}`>
```
#### `objectEntries`
Strictly typed `Object.entries`.
```ts
declare function objectEntries<T extends Record<any, any>>(obj: T): Array<[keyof T, T[keyof T]]>
```
#### `deepApply`
Deeply applies a callback to every key-value pair in the given object, as well as nested objects and arrays.
```ts
declare function deepApply<T extends Record<any, any>>(data: T, callback: (item: T, key: keyof T, value: T[keyof T]) => void): void
```
### Path
#### `withLeadingSlash`
Adds a leading slash to the given path if it does not already have one.
```ts
declare function withLeadingSlash(path?: string): string
```
#### `withoutTrailingSlash`
Removes the trailing slash from the given path if it has one.
```ts
declare function withoutTrailingSlash(path?: string): string
```
#### `withTrailingSlash`
Adds a trailing slash to the given path if it does not already have one.
```ts
declare function withTrailingSlash(path?: string): string
```
#### `joinURL`
Joins the given base URL and path, ensuring that there is only one slash between them.
```ts
declare function joinURL(base?: string, path?: string): string
```
#### `withoutBase`
Removes the base path from the input path, if it is present.
```ts
declare function withoutBase(input?: string, base?: string): string
```
#### `getPathname`
Returns the pathname of the given path, which is the path without the query string.
```ts
declare function getPathname(path?: string): string
```
### String
#### `template`
Simple template engine to replace variables in a string.
```ts
declare function template(str: string, variables: Record<string | number, any>, fallback?: string | ((key: string) => string)): string
```
**Example:**
```ts
import { template } from '@byjohann/utils'
const str = 'Hello, {name}!'
const variables = { name: 'world' }
console.log(template(str, variables)) // Hello, world!
```
#### `generateRandomId`
Generates a random string.
```ts
declare function generateRandomId(size?: number, dict?: string): string
```
#### `unindent`
Removes common leading whitespace from a template string while also removing empty lines at the beginning and end.
```ts
declare function unindent(str: TemplateStringsArray | string): string
```
## License
[MIT](./LICENSE) License © 2024-PRESENT [Johann Schopplich](https://github.com/johannschopplich)

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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