Skip to content

Commit

Permalink
Merge branch 'master' into extend-get-function-for-path-contain-quote
Browse files Browse the repository at this point in the history
  • Loading branch information
sodiray authored Feb 21, 2024
2 parents 2c6c12f + 241e6f8 commit 97c2a68
Show file tree
Hide file tree
Showing 11 changed files with 159 additions and 27 deletions.
22 changes: 15 additions & 7 deletions cdn/radash.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,9 @@ const boil = (array, compareFunc) => {
return null;
return array.reduce(compareFunc);
};
const sum = (array, fn) => {
return (array || []).reduce(
(acc, item) => acc + (fn ? fn(item) : item),
0
);
};
function sum(array, fn) {
return (array || []).reduce((acc, item) => acc + (fn ? fn(item) : item), 0);
}
const first = (array, defaultValue = void 0) => {
return array?.length > 0 ? array[0] : defaultValue;
};
Expand Down Expand Up @@ -588,6 +585,17 @@ const callable = (obj, fn) => {
});
};

function inRange(number, start, end) {
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;
}
return number >= Math.min(start, end) && number < Math.max(start, end);
}
const toFloat = (value, defaultValue) => {
const def = defaultValue === void 0 ? 0 : defaultValue;
if (value === null || value === void 0) {
Expand Down Expand Up @@ -927,4 +935,4 @@ const trim = (str, charsToTrim = " ") => {
return str.replace(regex, "");
};

export { all, alphabetical, assign, boil, callable, camel, capitalize, chain, clone, cluster, compose, construct, counting, crush, dash, debounce, defer, diff, draw, first, flat, fork, get, group, guard, intersects, invert, isArray, isDate, isEmpty, isEqual, isFloat, isFunction, isInt, isNumber, isObject, isPrimitive, isPromise, isString, isSymbol, iterate, keys, last, list, listify, lowerize, map, mapEntries, mapKeys, mapValues, max, memo, merge, min, objectify, omit, parallel, partial, partob, pascal, pick, proxied, random, range, reduce, replace, replaceOrAppend, retry, select, series, set, shake, shift, shuffle, sift, sleep, snake, sort, sum, template, throttle, title, toFloat, toInt, toggle, trim, tryit as try, tryit, uid, unique, upperize, zip, zipToObject };
export { all, alphabetical, assign, boil, callable, camel, capitalize, chain, clone, cluster, compose, construct, counting, crush, dash, debounce, defer, diff, draw, first, flat, fork, get, group, guard, inRange, intersects, invert, isArray, isDate, isEmpty, isEqual, isFloat, isFunction, isInt, isNumber, isObject, isPrimitive, isPromise, isString, isSymbol, iterate, keys, last, list, listify, lowerize, map, mapEntries, mapKeys, mapValues, max, memo, merge, min, objectify, omit, parallel, partial, partob, pascal, pick, proxied, random, range, reduce, replace, replaceOrAppend, retry, select, series, set, shake, shift, shuffle, sift, sleep, snake, sort, sum, template, throttle, title, toFloat, toInt, toggle, trim, tryit as try, tryit, uid, unique, upperize, zip, zipToObject };
21 changes: 15 additions & 6 deletions cdn/radash.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,9 @@ var radash = (function (exports) {
return null;
return array.reduce(compareFunc);
};
const sum = (array, fn) => {
return (array || []).reduce(
(acc, item) => acc + (fn ? fn(item) : item),
0
);
};
function sum(array, fn) {
return (array || []).reduce((acc, item) => acc + (fn ? fn(item) : item), 0);
}
const first = (array, defaultValue = void 0) => {
return array?.length > 0 ? array[0] : defaultValue;
};
Expand Down Expand Up @@ -591,6 +588,17 @@ var radash = (function (exports) {
});
};

function inRange(number, start, end) {
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;
}
return number >= Math.min(start, end) && number < Math.max(start, end);
}
const toFloat = (value, defaultValue) => {
const def = defaultValue === void 0 ? 0 : defaultValue;
if (value === null || value === void 0) {
Expand Down Expand Up @@ -955,6 +963,7 @@ var radash = (function (exports) {
exports.get = get;
exports.group = group;
exports.guard = guard;
exports.inRange = inRange;
exports.intersects = intersects;
exports.invert = invert;
exports.isArray = isArray;
Expand Down
5 changes: 3 additions & 2 deletions docs/array/cluster.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ split as evenly as possible.
```ts
import { cluster } from 'radash'

const gods = ['Ra', 'Zeus', 'Loki', 'Vishnu', 'Icarus', 'Osiris', 'Thor']
const gods = ['Ra', 'Zeus', 'Loki', 'Vishnu', 'Icarus', 'Osiris', 'Thor', 'Apollo', 'Artemis', 'Athena']

cluster(gods, 3)
// => [
// [ 'Ra', 'Zeus', 'Loki' ],
// [ 'Vishnu', 'Icarus', 'Osiris' ],
// [ 'Thor' ]
// ['Thor', 'Apollo', 'Artemis'],
// ['Athena']
// ]
```
22 changes: 22 additions & 0 deletions docs/number/in-range.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
title: inRange
description: Checks if the given number is between two numbers. The starting number is inclusive. The ending number is exclusive. The start and the end of the range can be ascending OR descending order. If end is not specified, it's set to start value. And start is set to 0.
group: Number
---

## Basic usage

Pass the number, the start and the end (optional) of the range. The `_.inRange` function will return true if the given number is in the range.

```ts
import { inRange } from 'radash'

inRange(10, 0, 20) // true
inRange(9.99, 0, 10) // true
inRange(Math.PI, 0, 3.15) // true
inRange(10, 10, 20) // true
inRange(10, 0, 10) // false

inRange(1, 2) // true
inRange(1, 0) // false
```
4 changes: 2 additions & 2 deletions docs/object/assign.mdx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
---
title: assign
description: Merges two objects together recursivly
description: Merges two objects together recursively
group: Object
---

## Basic usage

Merges two objects together recursivly into a new object applying values from right to left. Recursion only applies to child object properties.
Merges two objects together recursively into a new object applying values from right to left. Recursion only applies to child object properties.

```ts
import { assign } from 'radash'
Expand Down
14 changes: 8 additions & 6 deletions src/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,16 @@ export const boil = <T>(
* Sum all numbers in an array. Optionally provide a function
* to convert objects in the array to number values.
*/
export const sum = <T extends number | object>(
export function sum<T extends number>(array: readonly T[]): number
export function sum<T extends object>(
array: readonly T[],
fn: (item: T) => number
): number
export function sum<T extends object | number>(
array: readonly any[],
fn?: (item: T) => number
) => {
return (array || []).reduce(
(acc, item) => acc + (fn ? fn(item) : (item as number)),
0
)
): number {
return (array || []).reduce((acc, item) => acc + (fn ? fn(item) : item), 0)
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export {
proxied,
throttle
} from './curry'
export { toFloat, toInt } from './number'
export { inRange, toFloat, toInt } from './number'
export {
assign,
clone,
Expand Down
44 changes: 44 additions & 0 deletions src/number.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,47 @@
/**
* Checks if the given number is between zero (0) and the ending number. 0 is inclusive.
*
* * Numbers can be negative or positive.
* * Ending number is exclusive.
*
* @param {number} number The number to check.
* @param {number} end The end of the range. Exclusive.
* @returns {boolean} Returns `true` if `number` is in the range, else `false`.
*/
export function inRange(number: number, end: number): boolean

/**
* Checks if the given number is between two numbers.
*
* * Numbers can be negative or positive.
* * Starting number is inclusive.
* * Ending number is exclusive.
* * The start and the end of the range can be ascending OR descending order.
*
* @param {number} number The number to check.
* @param {number} start The start of the range. Inclusive.
* @param {number} end The end of the range. Exclusive.
* @returns {boolean} Returns `true` if `number` is in the range, else `false`.
*/
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
}

return number >= Math.min(start, end) && number < Math.max(start, end)
}

export const toFloat = <T extends number | null = number>(
value: any,
defaultValue?: T
Expand Down
4 changes: 2 additions & 2 deletions src/series.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ export const series = <T>(
)
/**
* Given two values in the series, returns the
* value that occurs later in the series
* value that occurs earlier in the series
*/
const min = (a: T, b: T): T => {
return indexesByKey[toKey(a)] < indexesByKey[toKey(b)] ? a : b
}
/**
* Given two values in the series, returns the
* value that occurs earlier in the series
* value that occurs later in the series
*/
const max = (a: T, b: T): T => {
return indexesByKey[toKey(a)] > indexesByKey[toKey(b)] ? a : b
Expand Down
2 changes: 1 addition & 1 deletion src/tests/array.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ describe('array module', () => {
assert.equal(result, 22)
})
test('gracefully handles null input list', () => {
const result = _.sum(null as unknown as readonly (number | object)[])
const result = _.sum(null as unknown as readonly number[])
assert.equal(result, 0)
})
})
Expand Down
46 changes: 46 additions & 0 deletions src/tests/number.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,51 @@ import { assert } from 'chai'
import * as _ from '..'

describe('number module', () => {
describe('inRange function', () => {
test('handles nullish values', () => {
assert.strictEqual(_.inRange(0, 1, null as any), false)
assert.strictEqual(_.inRange(0, null as any, 1), false)
assert.strictEqual(_.inRange(null as any, 0, 1), false)
assert.strictEqual(_.inRange(0, undefined as any, 1), false)
assert.strictEqual(_.inRange(undefined as any, 0, 1), false)

assert.strictEqual(_.inRange(0, 1, undefined as any), true)
})
test('handles bad input', () => {
const result = _.inRange(0, 1, {} as any)
assert.strictEqual(result, false)
})
test('computes correctly', () => {
assert.strictEqual(_.inRange(10, 0, 5), false)
assert.strictEqual(_.inRange(10, 0, 20), true)
assert.strictEqual(_.inRange(-10, 0, -20), true)
assert.strictEqual(_.inRange(9.99, 0, 10), true)
assert.strictEqual(_.inRange(Math.PI, 0, 3.15), true)
})
test('handles the different syntax of number type', () => {
assert.strictEqual(_.inRange(0, -1, 1), true)
assert.strictEqual(_.inRange(Number(0), -1, 1), true)
assert.strictEqual(_.inRange(+'0', -1, 1), true)
})
test('handles two params', () => {
assert.strictEqual(_.inRange(1, 2), true)
assert.strictEqual(_.inRange(1.2, 2), true)
assert.strictEqual(_.inRange(2, 1), false)
assert.strictEqual(_.inRange(2, 2), false)
assert.strictEqual(_.inRange(3.2, 2), false)
assert.strictEqual(_.inRange(-1, 1), false)
assert.strictEqual(_.inRange(-1, -10), true)
})
test('handles the exclusive end of the range', () => {
assert.strictEqual(_.inRange(1, 0, 1), false)
assert.strictEqual(_.inRange(10.0, 0, 10), false)
})
test('handles the inclusive start of the range', () => {
assert.strictEqual(_.inRange(0, 0, 1), true)
assert.strictEqual(_.inRange(10.0, 10, 20), true)
})
})

describe('toFloat function', () => {
test('handles null', () => {
const result = _.toFloat(null)
Expand All @@ -24,6 +69,7 @@ describe('number module', () => {
assert.strictEqual(result, 20.0)
})
})

describe('toInt function', () => {
test('handles null', () => {
const result = _.toInt(null)
Expand Down

0 comments on commit 97c2a68

Please sign in to comment.