some-javascript-utils
v1.0.1
Published
some functions for javascript
Downloads
11
Maintainers
Readme
some-javascript-utils
NOW WITH TYPESCRIPT (version: 1.0.0)
Note: if you want to use JavaScript uses "0.10.4"
Some functions to work better with arrays, objects and more!!
Functions
Arrays
sortBy
sortBy
How to import it?
import { Array } from "some-javascript-utils";
// import { sortBy } from "some-javascript-utils/array";
or
const { Array } from "some-javascript-utils";
// const { sortBy } = require("some-javascript-utils/array");
In Action!
🔶⚠Very important⚠🔶!!
just work with array of objects
// ... imports
const array = [{ id: 5 }, { id: 2 }, { id: 3 }, { id: 6 }];
Array.sortBy(array, "id", "asc") ;
// attribute "id" and "asc" are by default
// result
// [ { id: 2 }, { id: 3 }, { id: 5 }, { id: 6 } ]
Browser
getUserLanguage
How to import it?
import { Browser } from "some-javascript-utils";
// import { getUserLanguage } from "some-javascript-utils/Browser";
or
const { Browser } = "some-javascript-utils";
// const { getUserLanguage } = "some-javascript-utils/Browser";
In Action!
🔶⚠Very important⚠🔶!!
just work in browsers
// ... imports
const userLang = getUserLanguage();
// if pass a string parameter will save the language to a cookie ()
// example: "en"
parseQueries
How to import it?
import { Browser } from "some-javascript-utils";
// import { parseQueries } from "some-javascript-utils/Browser";
or
const { Browser } = "some-javascript-utils";
// const { parseQueries } = "some-javascript-utils/Browser";
In Action!
🔶⚠Very important⚠🔶!!
just work in browsers
// ...imports
//! using location from useLocation() hook from "react-router-dom"
useEffect(() => {
const { search } = location // ex: [some-url]?id=1&folder=sito
const queryParams = parseQueries(search)
// queryParams will value { id: "1", folder: "sito" }
},[location])
scrollTo
How to import it?
import { Browser } from "some-javascript-utils";
// import { scrollTo } from "some-javascript-utils"/Browser;
or
const { Browser } = "some-javascript-utils";
// const { scrollTo } = "some-javascript-utils/Browser";
In Action!
🔶⚠Very important⚠🔶!!
just work in browsers
// ... imports
// it's obviously how it works
scrollTo(0) // go to top of the page
createCookie
How to import it?
import { Browser } from "some-javascript-utils";
// import { createCookie } from "some-javascript-utils/Browser";
or
const { Browser } = "some-javascript-utils";
// const { createCookie } = "some-javascript-utils/Browser";
In Action!
🔶⚠Very important⚠🔶!!
just work in browsers
// ...imports
const name = "user";
const value = "sito";
const expiration = 765 // count of days or "2019-12-31 23:59:59"
createCookie(name, expiration, value);
// result
// a new cookie in document.cookie = `${name}=${value};${expires}";path=/`;
getCookie
How to import it?
import { Browser } from "some-javascript-utils";
// import { getCookie } from "some-javascript-utils/Browser";
or
const { Browser } = "some-javascript-utils";
// const { getCookie } = "some-javascript-utils/Browser";
In Action!
🔶⚠Very important⚠🔶!!
just work in browsers
// ...imports
const name = "user";
getCookie(name);
// result
// sito (of previous example)
deleteCookie
How to import it?
import { Browser } from "some-javascript-utils";
// import { deleteCookie } from "some-javascript-utils/Browser";
or
const { Browser } = "some-javascript-utils";
// const { deleteCookie } = "some-javascript-utils/Browser";
In Action!
🔶⚠Very important⚠🔶!!
just work in browsers
// ...imports
const name = "user";
deleteCookie(name);
// result
// (document.cookie = `${name}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;`);/
validation
To validates a storage key
How to import it?
import { Browser } from "some-javascript-utils";
// import { validation } from "some-javascript-utils/Browser";
or
const { Browser } = "some-javascript-utils";
// const { validation } = "some-javascript-utils/Browser";
In Action!
🔶⚠Very important⚠🔶!!
just work in browsers
// ...imports
localStorage.setItem("user", "yo");
validation("user", "local");
// result
// true
toSlug
To turn a string to url slug
How to import it?
import { toSlug } from "some-javascript-utils";
or
const { toSlug } = "some-javascript-utils";
In Action!
// ...imports
const urlName = toSlug("The Man on the Hills")
// urlName = the-man-on-the-hills