Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix!: remove handling of types not present in type signatures #70

Merged
merged 12 commits into from
Jan 26, 2025
10 changes: 9 additions & 1 deletion docs/object/clone.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ since: 12.1.0

### Usage

Creates a shallow copy of the given object/value.
Creates a shallow copy of the given object or array.

Notably, built-in objects like `Map`, `Set`, `Date`, and others are not supported.

```ts
import * as _ from 'radashi'
Expand All @@ -21,3 +23,9 @@ const gods = [ra]
_.clone(ra) // => copy of ra
_.clone(gods) // => copy of gods
```

### Differences from spread syntax

The `clone` function is similar to spread syntax (e.g. `{...obj}`), but it also preserves the prototype of the original object. If a `constructor` property exists on the original prototype, it will be called with the `new` keyword to create a new instance, and all properties will then be copied over to it.

Unlike spread syntax, `clone` also supports cloning arrays.
3 changes: 0 additions & 3 deletions src/array/alphabetical.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ export function alphabetical<T>(
getter: (item: T) => string,
direction: 'asc' | 'desc' = 'asc',
): T[] {
if (!array) {
return []
}
const asc = (a: T, b: T) => `${getter(a)}`.localeCompare(getter(b))
const dsc = (a: T, b: T) => `${getter(b)}`.localeCompare(getter(a))
return array.slice().sort(direction === 'desc' ? dsc : asc)
Expand Down
5 changes: 1 addition & 4 deletions src/array/boil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,5 @@ export function boil<T>(
array: readonly T[],
compareFunc: (a: T, b: T) => T,
): T | null {
if (!array || (array.length ?? 0) === 0) {
return null
}
return array.reduce(compareFunc)
return array.length ? array.reduce(compareFunc) : null
}
3 changes: 0 additions & 3 deletions src/array/counting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ export function counting<T, TId extends string | number | symbol>(
array: readonly T[],
identity: (item: T) => TId,
): Record<TId, number> {
if (!array) {
return {} as Record<TId, number>
}
return array.reduce(
(acc, item) => {
const id = identity(item)
Expand Down
9 changes: 0 additions & 9 deletions src/array/diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,6 @@ export function diff<T>(
identity: (item: T) => string | number | symbol = (t: T) =>
t as unknown as string | number | symbol,
): T[] {
if (!root?.length && !other?.length) {
return []
}
if (root?.length === undefined) {
return [...other]
}
if (!other?.length) {
return [...root]
}
const bKeys = other.reduce(
(acc, item) => {
acc[identity(item)] = true
Expand Down
2 changes: 1 addition & 1 deletion src/array/first.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ export function first<
): TArray extends readonly [infer TFirst, ...any[]]
? TFirst
: TArray[number] | TDefault {
return array?.length > 0 ? array[0] : defaultValue
return array.length > 0 ? array[0] : defaultValue
}
6 changes: 2 additions & 4 deletions src/array/fork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ export function fork<T>(
condition: (item: T) => boolean,
): [T[], T[]] {
const forked: [T[], T[]] = [[], []]
if (array) {
for (const item of array) {
forked[condition(item) ? 0 : 1].push(item)
}
for (const item of array) {
forked[condition(item) ? 0 : 1].push(item)
}
return forked
}
3 changes: 0 additions & 3 deletions src/array/intersects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ export function intersects<T, K>(
listB: readonly T[],
identity?: (t: T) => K,
): boolean {
if (!listA || !listB) {
return false
}
if (identity) {
const known = new Set(listA.map(identity))
return listB.some(item => known.has(identity(item)))
Expand Down
2 changes: 1 addition & 1 deletion src/array/last.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ export function last<
): TArray extends readonly [...any[], infer TLast]
? TLast
: TArray[number] | TDefault {
return array?.length > 0 ? array[array.length - 1] : defaultValue
return array.length > 0 ? array[array.length - 1] : defaultValue
}
12 changes: 0 additions & 12 deletions src/array/merge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,6 @@ export function merge<T>(
array: readonly T[],
toKey: (item: T) => any,
): T[] {
if (!array && !prev) {
return []
}
if (!array) {
return [...prev]
}
if (!prev) {
return []
}
if (!toKey) {
return [...prev]
}
const keys = new Map()
for (const item of array) {
keys.set(toKey(item), item)
Expand Down
14 changes: 7 additions & 7 deletions src/array/replace.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// biome-ignore lint/complexity/noBannedTypes: {} represents “all types but null/undefined”
type Defined<T> = T & ({} | null)

/**
* Replace an element in an array with a new item without modifying
* the array and return the new value.
Expand All @@ -10,18 +13,15 @@
* ```
* @version 12.1.0
*/
export function replace<T>(
export function replace<T, U>(
array: readonly T[],
newItem: T,
newItem: U,
match: (item: T, idx: number) => boolean,
): T[] {
if (!array) {
return []
}
): (T | Defined<U>)[] {
if (newItem === undefined) {
return [...array]
}
const out = array.slice()
const out = array.slice() as (T | Defined<U>)[]
for (let index = 0; index < array.length; index++) {
if (match(array[index], index)) {
out[index] = newItem
Expand Down
19 changes: 8 additions & 11 deletions src/array/replaceOrAppend.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// biome-ignore lint/complexity/noBannedTypes: {} represents “all types but null/undefined”
type Defined<T> = T & ({} | null)

/**
* Replace the first occurrence of an item in an array where the
* `match` function returns true. If no items match, append the new
Expand All @@ -14,21 +17,15 @@
* ```
* @version 12.1.0
*/
export function replaceOrAppend<T>(
export function replaceOrAppend<T, U>(
array: readonly T[],
newItem: T,
newItem: U,
match: (a: T, idx: number) => boolean,
): T[] {
if (!array && !newItem) {
return []
}
if (!newItem) {
): (T | Defined<U>)[] {
if (newItem === undefined) {
return [...array]
}
if (!array) {
return [newItem]
}
const out = array.slice()
const out = array.slice() as (T | Defined<U>)[]
for (let index = 0; index < array.length; index++) {
if (match(array[index], index)) {
out[index] = newItem
Expand Down
3 changes: 0 additions & 3 deletions src/array/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ export function select<T, U>(
mapper: (item: T, index: number) => U,
condition?: ((item: T, index: number) => boolean) | null,
): U[] {
if (!array) {
return []
}
let mapped: U
return array.reduce((acc, item, index) => {
if (condition) {
Expand Down
3 changes: 0 additions & 3 deletions src/array/selectFirst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ export function selectFirst<T, U>(
mapper: (item: T, index: number) => U,
condition?: (item: T, index: number) => boolean,
): U | undefined {
if (!array) {
return undefined
}
let foundIndex = -1
const found = array.find((item, index) => {
foundIndex = index
Expand Down
3 changes: 0 additions & 3 deletions src/array/sort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ export function sort<T>(
getter: (item: T) => number,
desc = false,
): T[] {
if (!array) {
return []
}
const asc = (a: T, b: T) => getter(a) - getter(b)
const dsc = (a: T, b: T) => getter(b) - getter(a)
return array.slice().sort(desc === true ? dsc : asc)
Expand Down
3 changes: 0 additions & 3 deletions src/array/toggle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ export function toggle<T>(
strategy?: 'prepend' | 'append'
},
): T[] {
if (!array) {
return item !== undefined ? [item] : []
}
if (item === undefined) {
return [...array]
}
Expand Down
3 changes: 0 additions & 3 deletions src/array/unzip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
* @version 12.2.0
*/
export function unzip<T>(arrays: readonly (readonly T[])[]): T[][] {
if (!arrays || !arrays.length) {
return []
}
const out = new Array(
arrays.reduce((max, arr) => Math.max(max, arr.length), 0),
)
Expand Down
4 changes: 0 additions & 4 deletions src/array/zipToObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ export function zipToObject<K extends string | number | symbol, V>(
keys: readonly K[],
values: V | ((key: K, idx: number) => V) | readonly V[],
): Record<K, V> {
if (!keys || !keys.length) {
return {} as Record<K, V>
}

const getValue = isFunction(values)
? values
: isArray(values)
Expand Down
3 changes: 0 additions & 3 deletions src/async/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ export async function map<T, K>(
array: readonly T[],
asyncMapFunc: (item: T, index: number) => PromiseLike<K>,
): Promise<K[]> {
if (!array) {
return []
}
const result = []
let index = 0
for (const value of array) {
Expand Down
3 changes: 0 additions & 3 deletions src/async/reduce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ export async function reduce<T, K>(
reducer: (acc: K, item: T, index: number) => Promise<K>,
initialValue?: K,
): Promise<K> {
if (!array) {
array = []
}
const indices = array.keys()
let acc = initialValue
// biome-ignore lint/style/noArguments:
Expand Down
15 changes: 1 addition & 14 deletions src/number/inRange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,7 @@ export function inRange(number: number, end: number): boolean
* ```
*/
export function inRange(number: number, start: number, end: number): boolean
export function inRange(number: number, start: number, end?: number): boolean {
const isTypeSafe =
typeof number === 'number' &&
typeof start === 'number' &&
(typeof end === 'undefined' || typeof end === 'number')

if (!isTypeSafe) {
return false
}

if (typeof end === 'undefined') {
end = start
start = 0
}

export function inRange(number: number, start: number, end = 0): boolean {
return number >= Math.min(start, end) && number < Math.max(start, end)
}
2 changes: 1 addition & 1 deletion src/number/max.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function max<T>(
array: readonly T[],
getter?: (item: T) => number,
): T | null {
if (!array || (array.length ?? 0) === 0) {
if (!array.length) {
return null
}
const get = getter ?? ((v: any) => v)
Expand Down
2 changes: 1 addition & 1 deletion src/number/min.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function min<T>(
array: readonly T[],
getter?: (item: T) => number,
): T | null {
if (!array || (array.length ?? 0) === 0) {
if (!array.length) {
return null
}
const get = getter ?? ((v: any) => v)
Expand Down
3 changes: 0 additions & 3 deletions src/object/assign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ export function assign<
TInitial extends Record<keyof any, any>,
TOverride extends Record<keyof any, any>,
>(initial: TInitial, override: TOverride): Assign<TInitial, TOverride> {
if (!initial || !override) {
return (initial ?? override ?? {}) as any
}
const proto = Object.getPrototypeOf(initial)
const merged = proto
? { ...initial }
Expand Down
14 changes: 1 addition & 13 deletions src/object/clone.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { isPrimitive } from 'radashi'

/**
* Creates a shallow copy of the given object/value.
*
Expand All @@ -16,17 +14,7 @@ import { isPrimitive } from 'radashi'
* ```
* @version 12.1.0
*/
export function clone<T>(obj: T): T {
// Primitive values do not need cloning.
if (isPrimitive(obj)) {
return obj
}

// Binding a function to an empty object creates a copy function.
if (typeof obj === 'function') {
return obj.bind({})
}

export function clone<T extends object>(obj: T): T {
const proto = Object.getPrototypeOf(obj)
const newObj =
typeof proto?.constructor === 'function'
Expand Down
3 changes: 0 additions & 3 deletions src/object/construct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ import { set } from 'radashi'
* @version 12.1.0
*/
export function construct<TObject extends object>(obj: TObject): object {
if (!obj) {
return {}
}
return Object.keys(obj).reduce((acc, path) => {
return set(acc, path, (obj as any)[path])
}, {})
Expand Down
3 changes: 0 additions & 3 deletions src/object/crush.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ import { type Intersect, isArray, isObject, type Simplify } from 'radashi'
* @version 12.1.0
*/
export function crush<T extends object>(value: T): Crush<T> {
if (!value) {
return {} as Crush<T>
}
return (function crushReducer(
crushed: Crush<T>,
value: unknown,
Expand Down
3 changes: 0 additions & 3 deletions src/object/invert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ export function invert<
TKey extends string | number | symbol,
TValue extends string | number | symbol,
>(obj: Record<TKey, TValue>): Record<TValue, TKey> {
if (!obj) {
return {} as Record<TValue, TKey>
}
const keys = Object.keys(obj) as TKey[]
return keys.reduce(
(acc, key) => {
Expand Down
3 changes: 0 additions & 3 deletions src/object/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ import { isArray, isPlainObject } from 'radashi'
* @version 12.1.0
*/
export function keys(value: object): string[] {
if (!value) {
return []
}
const keys: string[] = []
const keyPath: (string | number)[] = []
const recurse = (value: any) => {
Expand Down
9 changes: 1 addition & 8 deletions src/object/listify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,7 @@ export function listify<Value, Key extends string | number | symbol, Item>(
obj: Record<Key, Value>,
toItem: (key: Key, value: Value) => Item,
): Item[] {
if (!obj) {
return []
}
const entries = Object.entries(obj)
if (entries.length === 0) {
return []
}
return entries.reduce((acc, entry) => {
return Object.entries(obj).reduce((acc, entry) => {
acc.push(toItem(entry[0] as Key, entry[1] as Value))
return acc
}, [] as Item[])
Expand Down
Loading
Loading