Skip to content

Commit

Permalink
docs: add docs for new functions in v11.0.0 (#313)
Browse files Browse the repository at this point in the history
  • Loading branch information
sodiray authored Jun 27, 2023
1 parent 1817ace commit f791dcf
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 30 deletions.
42 changes: 28 additions & 14 deletions cdn/radash.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ const isNumber = (value) => {
const isDate = (value) => {
return Object.prototype.toString.call(value) === "[object Date]";
};
const isPromise = (value) => {
if (!value)
return false;
if (!value.then)
return false;
if (!isFunction(value.then))
return false;
return true;
};
const isEmpty = (value) => {
if (value === true || value === false)
return true;
Expand Down Expand Up @@ -144,7 +153,7 @@ const counting = (list2, identity) => {
const replace = (list2, newItem, match) => {
if (!list2)
return [];
if (!newItem)
if (newItem === void 0)
return [...list2];
for (let idx = 0; idx < list2.length; idx++) {
const item = list2[idx];
Expand Down Expand Up @@ -174,14 +183,14 @@ const select = (array, mapper, condition) => {
return acc;
}, []);
};
const max = (array, getter) => {
const get = getter ? getter : (v) => v;
function max(array, getter) {
const get = getter ?? ((v) => v);
return boil(array, (a, b) => get(a) > get(b) ? a : b);
};
const min = (array, getter) => {
const get = getter ? getter : (v) => v;
}
function min(array, getter) {
const get = getter ?? ((v) => v);
return boil(array, (a, b) => get(a) < get(b) ? a : b);
};
}
const cluster = (list2, size = 2) => {
const clusterCount = Math.ceil(list2.length / size);
return new Array(clusterCount).fill(null).map((_c, i) => {
Expand Down Expand Up @@ -453,9 +462,13 @@ const sleep = (milliseconds) => {
return new Promise((res) => setTimeout(res, milliseconds));
};
const tryit = (func) => {
return async (...args) => {
return (...args) => {
try {
return [void 0, await func(...args)];
const result = func(...args);
if (isPromise(result)) {
return result.then((value) => [void 0, value]).catch((err) => [err, void 0]);
}
return [void 0, result];
} catch (err) {
return [err, void 0];
}
Expand All @@ -467,10 +480,10 @@ const guard = (func, shouldGuard) => {
throw err;
return void 0;
};
const isPromise = (result) => result instanceof Promise;
const isPromise2 = (result) => result instanceof Promise;
try {
const result = func();
return isPromise(result) ? result.catch(_guard) : result;
return isPromise2(result) ? result.catch(_guard) : result;
} catch (err) {
return _guard(err);
}
Expand Down Expand Up @@ -868,15 +881,16 @@ const camel = (str) => {
return `${acc}${part.charAt(0).toUpperCase()}${part.slice(1)}`;
});
};
const snake = (str) => {
const snake = (str, options) => {
const parts = str?.replace(/([A-Z])+/g, capitalize).split(/(?=[A-Z])|[\.\-\s_]/).map((x) => x.toLowerCase()) ?? [];
if (parts.length === 0)
return "";
if (parts.length === 1)
return parts[0];
return parts.reduce((acc, part) => {
const result = parts.reduce((acc, part) => {
return `${acc}_${part.toLowerCase()}`;
});
return options?.splitOnNumber === false ? result : result.replace(/([A-Za-z]{1}[0-9]{1})/, (val) => `${val[0]}_${val[1]}`);
};
const dash = (str) => {
const parts = str?.replace(/([A-Z])+/g, capitalize)?.split(/(?=[A-Z])|[\.\-\s_]/).map((x) => x.toLowerCase()) ?? [];
Expand Down Expand Up @@ -912,4 +926,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, 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, 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 };
41 changes: 28 additions & 13 deletions cdn/radash.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ var radash = (function (exports) {
const isDate = (value) => {
return Object.prototype.toString.call(value) === "[object Date]";
};
const isPromise = (value) => {
if (!value)
return false;
if (!value.then)
return false;
if (!isFunction(value.then))
return false;
return true;
};
const isEmpty = (value) => {
if (value === true || value === false)
return true;
Expand Down Expand Up @@ -147,7 +156,7 @@ var radash = (function (exports) {
const replace = (list2, newItem, match) => {
if (!list2)
return [];
if (!newItem)
if (newItem === void 0)
return [...list2];
for (let idx = 0; idx < list2.length; idx++) {
const item = list2[idx];
Expand Down Expand Up @@ -177,14 +186,14 @@ var radash = (function (exports) {
return acc;
}, []);
};
const max = (array, getter) => {
const get = getter ? getter : (v) => v;
function max(array, getter) {
const get = getter ?? ((v) => v);
return boil(array, (a, b) => get(a) > get(b) ? a : b);
};
const min = (array, getter) => {
const get = getter ? getter : (v) => v;
}
function min(array, getter) {
const get = getter ?? ((v) => v);
return boil(array, (a, b) => get(a) < get(b) ? a : b);
};
}
const cluster = (list2, size = 2) => {
const clusterCount = Math.ceil(list2.length / size);
return new Array(clusterCount).fill(null).map((_c, i) => {
Expand Down Expand Up @@ -456,9 +465,13 @@ var radash = (function (exports) {
return new Promise((res) => setTimeout(res, milliseconds));
};
const tryit = (func) => {
return async (...args) => {
return (...args) => {
try {
return [void 0, await func(...args)];
const result = func(...args);
if (isPromise(result)) {
return result.then((value) => [void 0, value]).catch((err) => [err, void 0]);
}
return [void 0, result];
} catch (err) {
return [err, void 0];
}
Expand All @@ -470,10 +483,10 @@ var radash = (function (exports) {
throw err;
return void 0;
};
const isPromise = (result) => result instanceof Promise;
const isPromise2 = (result) => result instanceof Promise;
try {
const result = func();
return isPromise(result) ? result.catch(_guard) : result;
return isPromise2(result) ? result.catch(_guard) : result;
} catch (err) {
return _guard(err);
}
Expand Down Expand Up @@ -871,15 +884,16 @@ var radash = (function (exports) {
return `${acc}${part.charAt(0).toUpperCase()}${part.slice(1)}`;
});
};
const snake = (str) => {
const snake = (str, options) => {
const parts = str?.replace(/([A-Z])+/g, capitalize).split(/(?=[A-Z])|[\.\-\s_]/).map((x) => x.toLowerCase()) ?? [];
if (parts.length === 0)
return "";
if (parts.length === 1)
return parts[0];
return parts.reduce((acc, part) => {
const result = parts.reduce((acc, part) => {
return `${acc}_${part.toLowerCase()}`;
});
return options?.splitOnNumber === false ? result : result.replace(/([A-Za-z]{1}[0-9]{1})/, (val) => `${val[0]}_${val[1]}`);
};
const dash = (str) => {
const parts = str?.replace(/([A-Z])+/g, capitalize)?.split(/(?=[A-Z])|[\.\-\s_]/).map((x) => x.toLowerCase()) ?? [];
Expand Down Expand Up @@ -952,6 +966,7 @@ var radash = (function (exports) {
exports.isNumber = isNumber;
exports.isObject = isObject;
exports.isPrimitive = isPrimitive;
exports.isPromise = isPromise;
exports.isString = isString;
exports.isSymbol = isSymbol;
exports.iterate = iterate;
Expand Down
2 changes: 1 addition & 1 deletion cdn/radash.min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/async/tryit.mdx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
---
title: tryit
group: 'Async'
description: Convert a function to an error-first async function
description: Convert a function to an error-first function
---

## Basic usage

Error-first callbacks were cool. Using mutable variables to hoist state when doing try/catch was not cool.

The `tryit` function let's you wrap a function to convert it to an error-first async function.
The `tryit` function let's you wrap a function to convert it to an error-first function. **Works for both async and sync functions.**

```ts
import { tryit } from 'radash'
Expand Down
10 changes: 10 additions & 0 deletions docs/string/snake.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,13 @@ import { snake } from 'radash'

snake('green fish blue fish') // => green_fish_blue_fish
```

**Warning**: In v11.0.0 a change was made to _fix_ this function so that it correctly splits numbers from neighbouring letters (`hello5` becomes `hello_5`). You can opt out of this behavior and continue with the legacy style (`hello5` becomes `hello5`) by passing the `splitOnNumber` options.

```ts
snake('5green fish 2blue fish') // => 5_green_fish_2_blue_fish

snake('5green fish 2blue fish', {
splitOnNumber: false
}) // => 5green_fish_2blue_fish
```
17 changes: 17 additions & 0 deletions docs/typed/is-promise.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
title: isPromise
description: 'Determine if a value is a Promise'
group: Typed
---

## Basic usage

Pass in a value and get a boolean telling you if the value is a Promise. This function is not _"bullet proof"_ because determining if a value is a Promise in javascript is not _"bullet proof"_. The standard/recommended method is to use `Promise.resolve` to essentially cast any value, promise or not, into an awaited value. However, this may do in a pinch.

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

isPromise('hello') // => false
isPromise(['hello']) // => false
isPromise(new Promise(res => res())) // => true
```

1 comment on commit f791dcf

@vercel
Copy link

@vercel vercel bot commented on f791dcf Jun 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.