diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 9ebffdf..243684c 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -15,14 +15,14 @@ series [How to Contribute to an Open Source Project on GitHub][egghead] > pull requests from branches on your fork. To do this, run: > > ``` -> git remote add upstream https://github.com/@feedzai/js-utilities +> git remote add upstream https://github.com/feedzai/js-utilities > git fetch upstream -> git branch --set-upstream-to=upstream/master master +> git branch --set-upstream-to=upstream/main main > ``` > > This will add the original repository as a "remote" called "upstream," Then > fetch the git information from that remote, then set your local `main` -> branch to use the upstream master branch whenever you run `git pull`. Then you +> branch to use the upstream main branch whenever you run `git pull`. Then you > can make all of your pull request branches based on this `main` branch. > Whenever you want to update your version of `main`, do a regular `git pull`. @@ -40,5 +40,5 @@ requests! Thanks! [egghead]: https://egghead.io/series/how-to-contribute-to-an-open-source-project-on-github -[issues]: https://github.com/@feedzai/js-utilities/issues +[issues]: https://github.com/feedzai/js-utilities/issues diff --git a/README.md b/README.md index 15952a7..37a3aff 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ It also comes with comprehensive documentation, making it a breeze to navigate a The package is available in both CommonJS and ES Modules formats so, whether you're working in a traditional environment or embracing the latest ES Modules, we have you covered. -For JavaScript and TypeScript developers seeking a lightweight but robust toolkit that balances simplicity with utility, `@feedzai/js-utilities` might jus be the answer! +For JavaScript and TypeScript developers seeking a lightweight but robust toolkit that balances simplicity with utility, `@feedzai/js-utilities` might just be the answer! ## Table of Contents @@ -168,6 +168,7 @@ import * as JS_UTILS from "@feedzai/js-utilities"; | Name | Source | | ------------------ | ------------------------------------------------------------------------------------------------------- | +| formatDate | [source](https://github.com/feedzai/js-utilities/blob/main/src/functions/dates/format-date.ts) | | getBrowserTimezone | [source](https://github.com/feedzai/js-utilities/blob/main/src/functions/dates/get-browser-timezone.ts) | ### Events @@ -252,6 +253,7 @@ import * as JS_UTILS from "@feedzai/js-utilities"; | ------------------ | -------------------------------------------------------------------------------------------------------- | | callIfExists | [source](https://github.com/feedzai/js-utilities/blob/main/src/functions/utilities/call-if-exists.ts) | | emptyFunction | [source](https://github.com/feedzai/js-utilities/blob/main/src/functions/utilities/empty-function.ts) | +| generateUUID | [source](https://github.com/feedzai/js-utilities/blob/main/src/functions/utilities/generate-uuid.ts) | | makeCancelable | [source](https://github.com/feedzai/js-utilities/blob/main/src/functions/utilities/make-cancelable.ts) | | throwError | [source](https://github.com/feedzai/js-utilities/blob/main/src/functions/utilities/throw-error.ts) | | HTTP StatusCodes | [source](https://github.com/feedzai/js-utilities/blob/main/src/functions/constants/http-status-codes.ts) | @@ -271,6 +273,8 @@ import * as JS_UTILS from "@feedzai/js-utilities"; | useConstant | [source](https://github.com/feedzai/js-utilities/tree/main/src/hooks/use-constant.ts) | | useCopyToClipboard | [source](https://github.com/feedzai/js-utilities/tree/main/src/hooks/use-copy-to-clipboard.ts) | | useEffectOnce | [source](https://github.com/feedzai/js-utilities/tree/main/src/hooks/use-effect-once.ts) | +| useEnsuredForwardedRef | [source](https://github.com/feedzai/js-utilities/tree/main/src/hooks/use-ensured-forwarded-ref.ts) | +| useIntersection | [source](https://github.com/feedzai/js-utilities/tree/main/src/hooks/use-intersection.ts) | | useLifecycle | [source](https://github.com/feedzai/js-utilities/tree/main/src/hooks/use-lifecycle.ts) | | useLiveRef | [source](https://github.com/feedzai/js-utilities/tree/main/src/hooks/use-live-ref.ts) | | useMergeRefs | [source](https://github.com/feedzai/js-utilities/tree/main/src/hooks/use-merge-refs.ts) | @@ -288,13 +292,13 @@ To run tests locally: ``` # using npm -npm run test:open +npm run test:unit # using npm (headless) npm run test # using yarn -yarn test:open +yarn test:unit # using yarn (headless) yarn test diff --git a/cypress/test/functions/array.cy.ts b/cypress/test/functions/array.cy.ts index 62552a3..794e958 100644 --- a/cypress/test/functions/array.cy.ts +++ b/cypress/test/functions/array.cy.ts @@ -265,6 +265,12 @@ describe("flatten", () => { const result = _.flatten(array); expect(result).to.deep.equal([]); }); + + it("should fully flatten when level exceeds the nesting depth", () => { + const array = [1, [2, [3, [4]], 5]]; + const result = _.flatten(array, 5); + expect(result).to.deep.equal([1, 2, 3, 4, 5]); + }); }); describe("find", () => { diff --git a/cypress/test/functions/typed.cy.ts b/cypress/test/functions/typed.cy.ts index 4b47656..2885c08 100644 --- a/cypress/test/functions/typed.cy.ts +++ b/cypress/test/functions/typed.cy.ts @@ -126,6 +126,14 @@ describe("isObject", () => { it("should return false for a new String('')", () => { expect(_.isObject(new String(""))).to.be.false; }); + + it("should return false for an array", () => { + expect(_.isObject([1, 2, 3])).to.be.false; + }); + + it("should return false for an arrow function", () => { + expect(_.isObject(() => {})).to.be.false; + }); }); describe("isPlainObject", () => { diff --git a/docs/docs/functions/arbitrarily/draw.mdx b/docs/docs/functions/arbitrarily/draw.mdx index 4430434..4619eb2 100644 --- a/docs/docs/functions/arbitrarily/draw.mdx +++ b/docs/docs/functions/arbitrarily/draw.mdx @@ -8,7 +8,7 @@ This helper can also be useful for using inside tests where - given a list of po ## API ```typescript -function random(lower?: number, upper?: number | boolean, floating?: boolean): number; +function draw(array: readonly T[]): T | null; ``` ### Usage diff --git a/docs/docs/functions/arrays/flatten.mdx b/docs/docs/functions/arrays/flatten.mdx index bb52af0..384c533 100644 --- a/docs/docs/functions/arrays/flatten.mdx +++ b/docs/docs/functions/arrays/flatten.mdx @@ -19,13 +19,12 @@ const array = [1, [2, [3, [4]], 5]] flatten(array, true) // => [1, 2, 3, 4, 5] -flattenDepth(array, 1) +flatten(array, 1) // => [1, 2, [3, [4]], 5] -flattenDepth(array, 2) +flatten(array, 2) // => [1, 2, 3, [4], 5] -flatten(array, 5]]) -// => [1, 2, [3, [4]], 5] - +flatten(array, 5) +// => [1, 2, 3, 4, 5] ``` diff --git a/docs/docs/functions/arrays/remove-index.mdx b/docs/docs/functions/arrays/remove-index.mdx index 0c89f45..06c5589 100644 --- a/docs/docs/functions/arrays/remove-index.mdx +++ b/docs/docs/functions/arrays/remove-index.mdx @@ -23,7 +23,7 @@ const DATA = [ "Maggie" ]; -const SORTED = removeIndex(1); +const RESULT = removeIndex(DATA, 1); -console.log(SORTED) // ["Homer","Lisa","Bart","Maggie"]; +console.log(RESULT) // ["Homer","Lisa","Bart","Maggie"]; ``` diff --git a/docs/docs/functions/arrays/remove-item.mdx b/docs/docs/functions/arrays/remove-item.mdx index 3530d3a..218c149 100644 --- a/docs/docs/functions/arrays/remove-item.mdx +++ b/docs/docs/functions/arrays/remove-item.mdx @@ -22,7 +22,7 @@ const DATA = [ "Maggie" ]; -const SORTED = removeItem("Marge"); +const RESULT = removeItem(DATA, "Marge"); -console.log(SORTED) // ["Homer","Lisa","Bart","Maggie"]; +console.log(RESULT) // ["Homer","Lisa","Bart","Maggie"]; ``` diff --git a/docs/docs/functions/objects/get.mdx b/docs/docs/functions/objects/get.mdx index 5fd0637..89c0d12 100644 --- a/docs/docs/functions/objects/get.mdx +++ b/docs/docs/functions/objects/get.mdx @@ -25,7 +25,7 @@ get(complexObject, 'a[0].bar.c') // => 3 get(complexObject, ['a', '0', 'bar', 'c']) -// => 2 +// => 3 get(simpleObject, 'a.bar.c', 'default') // => 'default' diff --git a/docs/docs/functions/objects/is-equal.mdx b/docs/docs/functions/objects/is-equal.mdx index c5e0cc8..bf15cb3 100644 --- a/docs/docs/functions/objects/is-equal.mdx +++ b/docs/docs/functions/objects/is-equal.mdx @@ -12,7 +12,7 @@ function isEqual(x: TType, y: TType): boolean; ### Usage ```typescript -import { has } from '@feedzai/js-utilities'; +import { isEqual } from '@feedzai/js-utilities'; isEqual(null, null) // => true diff --git a/docs/docs/functions/string/kebab-case.mdx b/docs/docs/functions/string/kebab-case.mdx index 04ddac7..1d883b3 100644 --- a/docs/docs/functions/string/kebab-case.mdx +++ b/docs/docs/functions/string/kebab-case.mdx @@ -12,7 +12,7 @@ function kebabCase(str: string): string; ### Usage ```typescript -import { random } from '@feedzai/js-utilities'; +import { kebabCase } from '@feedzai/js-utilities'; kebabCase('Foo Bar') // => 'foo-bar' diff --git a/docs/docs/functions/string/readable-string-list.mdx b/docs/docs/functions/string/readable-string-list.mdx index d90f0f0..21b8158 100644 --- a/docs/docs/functions/string/readable-string-list.mdx +++ b/docs/docs/functions/string/readable-string-list.mdx @@ -32,7 +32,7 @@ readableStringList(SIMPSONS, "fr", "and"); // 'Homer, Marge, Lisa, Bart et Maggie' readableStringList(SIMPSONS, "fr", "or"); -// 'Homer, Marge, Lisa, Bart our Maggie' +// 'Homer, Marge, Lisa, Bart ou Maggie' readableStringList(SIMPSONS, "pt", "and"); // 'Homer, Marge, Lisa, Bart e Maggie' diff --git a/docs/docs/functions/string/strip-unit.mdx b/docs/docs/functions/string/strip-unit.mdx index ffe4401..f116202 100644 --- a/docs/docs/functions/string/strip-unit.mdx +++ b/docs/docs/functions/string/strip-unit.mdx @@ -1,7 +1,7 @@ --- title: stripUnit --- -Shortens a given number to its shortest form so it can occupy little space as possible. +Removes CSS units from a string-based value, returning the numeric value and the unit separately. ## API diff --git a/docs/docs/functions/string/template.mdx b/docs/docs/functions/string/template.mdx index fff2f1e..eea68c1 100644 --- a/docs/docs/functions/string/template.mdx +++ b/docs/docs/functions/string/template.mdx @@ -13,7 +13,7 @@ function template(str: string, data: Record, regex?: RegExp): strin ### Usage ```typescript -import { titleCase } from '@feedzai/js-utilities'; +import { template } from '@feedzai/js-utilities'; template('Hello, {{name}}', { name: 'ray' }) //-> Hello, ray diff --git a/docs/docs/functions/typed/is-date.mdx b/docs/docs/functions/typed/is-date.mdx index 5baf1f5..06a0729 100644 --- a/docs/docs/functions/typed/is-date.mdx +++ b/docs/docs/functions/typed/is-date.mdx @@ -15,13 +15,13 @@ function isDate(value: any): value is Date; ```typescript import { isDate } from '@feedzai/js-utilities'; -isDate(new Date())) +isDate(new Date()) //-> true isDate("2024-02-18") //-> false -isDate(123)) +isDate(123) //-> false isDate([2024, 2, 18]) diff --git a/docs/docs/functions/typed/is-element.mdx b/docs/docs/functions/typed/is-element.mdx index 518e9f7..78ff135 100644 --- a/docs/docs/functions/typed/is-element.mdx +++ b/docs/docs/functions/typed/is-element.mdx @@ -12,7 +12,7 @@ function isElement(value: any): value is Element; ### Usage ```typescript -import { isUndefined } from '@feedzai/js-utilities'; +import { isElement } from '@feedzai/js-utilities'; isElement(document.body) // true diff --git a/docs/docs/functions/typed/is-function.mdx b/docs/docs/functions/typed/is-function.mdx index 974aee5..cb91eaa 100644 --- a/docs/docs/functions/typed/is-function.mdx +++ b/docs/docs/functions/typed/is-function.mdx @@ -1,7 +1,7 @@ --- title: isFunction --- -Checks if `value` is classified as a String primitive or object. +Checks if `value` is classified as a Function object. ## API diff --git a/docs/docs/functions/typed/is-object.mdx b/docs/docs/functions/typed/is-object.mdx index 8f2f664..0b75810 100644 --- a/docs/docs/functions/typed/is-object.mdx +++ b/docs/docs/functions/typed/is-object.mdx @@ -19,10 +19,10 @@ isObject({}) // true isObject([1, 2, 3]) -// true +// false isObject(() => {}) -// true +// false isObject(null) // false diff --git a/docs/docs/hooks/use-click-outside.mdx b/docs/docs/hooks/use-click-outside.mdx index d46dfb3..daf0e03 100644 --- a/docs/docs/hooks/use-click-outside.mdx +++ b/docs/docs/hooks/use-click-outside.mdx @@ -21,7 +21,7 @@ function App(props) { const DROPDOWN_REF = useRef(); useClickOutside( - DROPDROWN_REF, + DROPDOWN_REF, () => props.closeDropdown(), props.shouldClose, ); diff --git a/docs/docs/hooks/use-ensured-forwared-ref.mdx b/docs/docs/hooks/use-ensured-forwarded-ref.mdx similarity index 100% rename from docs/docs/hooks/use-ensured-forwared-ref.mdx rename to docs/docs/hooks/use-ensured-forwarded-ref.mdx diff --git a/docs/docs/intro.mdx b/docs/docs/intro.mdx index ffbf55a..eb62f29 100644 --- a/docs/docs/intro.mdx +++ b/docs/docs/intro.mdx @@ -16,7 +16,7 @@ It also comes with comprehensive documentation, making it a breeze to navigate a The package is available in both CommonJS and ES Modules formats so, whether you're working in a traditional environment or embracing the latest ES Modules, we have you covered. -For JavaScript and TypeScript developers seeking a lightweight but robust toolkit that balances simplicity with utility, `@feedzai/js-utilities` might jus be the answer! +For JavaScript and TypeScript developers seeking a lightweight but robust toolkit that balances simplicity with utility, `@feedzai/js-utilities` might just be the answer! ## Installation diff --git a/src/functions/DOM/toggle-data-attribute.ts b/src/functions/DOM/toggle-data-attribute.ts index c989307..258fa3b 100644 --- a/src/functions/DOM/toggle-data-attribute.ts +++ b/src/functions/DOM/toggle-data-attribute.ts @@ -5,7 +5,7 @@ */ /** - * Adds a data attribute to a DOM element, without its state bein rendered as a value + * Adds a data attribute to a DOM element, without its state being rendered as a value * * @example * diff --git a/src/functions/array/flatten.ts b/src/functions/array/flatten.ts index e6f63c9..dc9827d 100644 --- a/src/functions/array/flatten.ts +++ b/src/functions/array/flatten.ts @@ -13,16 +13,16 @@ * * const array = [1, [2, [3, [4]], 5]] * - * flatten(array, 5]]) - * // => [1, 2, [3, [4]], 5] + * flatten(array, 5) + * // => [1, 2, 3, 4, 5] * * flatten(array, true) * // => [1, 2, 3, 4, 5] * - * flattenDepth(array, 1) + * flatten(array, 1) * // => [1, 2, [3, [4]], 5] * - * flattenDepth(array, 2) + * flatten(array, 2) * // => [1, 2, 3, [4], 5] */ export function flatten(arr: T[], level?: number | boolean) { diff --git a/src/functions/object/get.ts b/src/functions/object/get.ts index 5ad06ba..cd7a669 100644 --- a/src/functions/object/get.ts +++ b/src/functions/object/get.ts @@ -24,7 +24,7 @@ import { isNil } from ".."; * // => 3 * * get(complexObject, ['a', '0', 'bar', 'c']) - * // => 2 + * // => 3 * * get(simpleObject, 'a.bar.c', 'default') * // => 'default' diff --git a/src/functions/typed/is-object.ts b/src/functions/typed/is-object.ts index 25f01aa..a1c00d8 100644 --- a/src/functions/typed/is-object.ts +++ b/src/functions/typed/is-object.ts @@ -15,10 +15,10 @@ * // true * * isObject([1, 2, 3]) - * // true + * // false * * isObject(() => {}) - * // true + * // false * * isObject(null) * // false diff --git a/src/functions/utilities/generate-uuid.ts b/src/functions/utilities/generate-uuid.ts index e18162c..d893b23 100644 --- a/src/functions/utilities/generate-uuid.ts +++ b/src/functions/utilities/generate-uuid.ts @@ -61,7 +61,7 @@ function generateFallbackUUID() { * * @example * ```ts - * import { generateUUID } from "@feedzai/react-utilities"; + * import { generateUUID } from "@feedzai/js-utilities"; * * const uuid = generateUUID(); // "123e4567-e89b-12d3-a456-426614174000" * ``` diff --git a/src/hooks/use-script.ts b/src/hooks/use-script.ts index 06de507..64c10f2 100644 --- a/src/hooks/use-script.ts +++ b/src/hooks/use-script.ts @@ -15,9 +15,9 @@ const cachedScripts: string[] = []; * * @example * ```tsx - * import { useSafeLayoutEffect } from '@feedzai/js-utilities/hooks'; + * import { useScript } from '@feedzai/js-utilities/hooks'; * ... - * const [loaded, error] = useScript("") + * const [loaded, error] = useScript('') * ``` */ export function useScript(