regex-safe
v0.2.23
Published
Easy controls with Regex.
Downloads
156
Maintainers
Readme
Regex Safe
Easy controls with Regex.
Use of Utils
Equal
import { isEqual } from "regex-safe";
isEqual({ foo: "bar" }, { foo: "bar" }); // true
isEqual({ foo: "bar" }, { bar: "foo" }); // false
Mul
import { mul } from "regex-safe";
mul(1, 2, 3, 4); // 24
Digitize
import { digitize } from "regex-safe";
digitize(123); // [1, 2, 3]
Dec to BI
import { decToBi } from "regex-safe";
decToBi(10); //1010
toChars
import { toChars } from "regex-safe";
toChars(0); // A
toChars(1); // B
toChars(25); // Z
GCD
import { gcd } from "regex-safe";
const gcd = (a, b) => (b === 0 ? a : gcd(b, a % b));
Clamp
import { clamp } from "regex-safe";
clamp(199, 10, 25); // 25
Remainder
import { remainder } from "regex-safe";
remainder(1, 2, 3, 4); // 1
Mod
import { mod } from "regex-safe";
mod(-1, 5); // 4
mod(3, 5); // 3
mod(6, 5); // 1
Factorial
import { factorial } from "regex-safe";
factorial(2); // 2
factorial(3); // 6
factorial(4); // 24
factorial(5); // 120
factorial(6); // 720
Division
import { division } from "regex-safe";
division(1, 2, 3, 4); // 0.04166666666666666
Average
import { average } from "regex-safe";
average(1, 2, 3, 4); // 2.5
Xor
import { xor } from "regex-safe";
xor(true, true); // false
xor(false, false); // false
xor(true, false); // true
xor(false, true); // true
Curry
import { curry } from "regex-safe";
const sum = (a, b, c) => a + b + c;
curry(sum)(1)(2)(3); // 6
curry(sum)(1, 2, 3); // 6
curry(sum, 1)(2, 3); // 6
curry(sum, 1)(2)(3); // 6
curry(sum, 1, 2)(3); // 6
curry(sum, 1, 2, 3); // 6
Unary
import { unary } from "regex-safe";
["1", "2", "3", "4", "5"].map(unary(parseInt)); // [1, 2, 3, 4, 5]
Get Position
import { getPosition } from "regex-safe";
getPosition(document.body); // { left: 0, top: 0 }
Sort Descending
import { sortDescending } from "regex-safe";
sortDescending([new Date("2019-01-03"), new Date("2019-01-01")]); // [new Date("2019-01-03"), new Date("2019-01-01")]
Yesterday
import { yesterday } from "regex-safe";
yesterday(); // 2022-07-10T14:32:24.326Z
Get Week Day
import { getWeekday } from "regex-safe";
getWeekday(new Date()); // sunday
Number of Days
import { numberOfDays } from "regex-safe";
numberOfDays(2022); // 365
Tomorrow
import { tomorrow } from "regex-safe";
tomorrow(); // 2022-07-10T17:34:28.400Z
Get Timezone
import { getTimezone } from "regex-safe";
getTimezone(); // Europe/Istanbul
Days in Month
import { daysInMonth } from "regex-safe";
daysInMonth(2020, 1); // 30
Get Month Name
import { getMonthName } from "regex-safe";
getMonthName(new Date()); // July
Get Last Date
import { getLastDate } from "regex-safe";
getLastDate(new Date("2020-01-01")); // 2020-01-30T21:00:00.000Z
Get First Date
import { getFirstDate } from "regex-safe";
getFirstDate(); // 2022-06-30T21:00:00.000Z
getFirstDate(new Date("2020-01-01")); // 2020-01-01T00:00:00.
Day of Year
import { dayOfYear } from "regex-safe";
dayOfYear(new Date(2020, 04, 16)); // 137
Format Date
import { formatLocaleDate } from "regex-safe";
formatLocaleDate(new Date(), "pt-BR"); // 06/05/2020
formatLocaleDate(new Date(), "tr-TR"); // 06.05.2020
Format Seconds
import { formatSeconds } from "regex-safe";
formatSeconds(200); // 00:03:20
Month Diff
import { monthDiff } from "regex-safe";
monthDiff(new Date("2020-01-01"), new Date("2021-01-01")); // 12
Diff Days
import { diffDays } from "regex-safe";
diffDays(new Date("2014-12-19"), new Date("2020-01-01")); // 1839
Suffix Am / Pm
import { suffixAmPm } from "regex-safe";
suffixAmPm(0); // '12am'
suffixAmPm(12); // '12pm'
Transpose
import { swapItems } from "regex-safe";
swapItems([1, 2, 3, 4, 5], 1, 4); // [1, 5, 3, 4, 2]
Transpose
import { transpose } from "regex-safe";
transpose([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
]); // [ [ 1, 4, 7 ], [ 2, 5, 8 ], [ 3, 6, 9 ] ]
Chunk
import { chunk } from "regex-safe";
chunk([1, 2, 3, 4, 5, 6, 7, 8], 4); // [[1, 2, 3, 4], [5, 6, 7, 8]]
Sort
import { sort } from "regex-safe";
sort([1, 5, 2, 4, 3]); // [1, 2, 3, 4, 5]
Shuffle
import { shuffle } from "regex-safe";
shuffle([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); // [9, 1, 10, 6, 8, 5, 2, 3, 7, 4]
Repeat
import { repeat } from "regex-safe";
repeat([1, 2, 3], 3); // [1, 2, 3, 1, 2, 3, 1, 2, 3]```
Union
import { removeFalsy } from "regex-safe";
removeFalsy([0, "mehmet", "", NaN, true, 5, undefined, "regex-safe", false]); // [ 'mehmet', true, 5, 'regex-safe' ]
Group by
import { groupBy } from "regex-safe";
groupBy(
[
{ branch: "audi", model: "q8", year: "2019" },
{ branch: "audi", model: "rs7", year: "2020" },
{ branch: "ford", model: "mustang", year: "2019" },
{ branch: "ford", model: "explorer", year: "2020" },
{ branch: "bmw", model: "x7", year: "2020" },
],
"branch"
); // { audi: [{ branch: "audi", model: "q8", year: "2019" }, { branch: "audi", model: "rs7", year: "2020" }], ford: [{ branch: "ford", model: "mustang", year: "2019" }, { branch: "ford", model: "explorer", year: "2020" }], bmw: [{ branch: "bmw", model: "x7", year: "2020" }] }
Union
import { union } from "regex-safe";
union([1, 2], [2, 3], [3]); // [1, 2, 3]
Unique
import { unique } from "regex-safe";
unique([1, 2, 3, 1, 2, 3, 4, 5]); // [1, 2, 3, 4, 5]
Sum
import { sum } from "regex-safe";
sum([1, 2, 3, 4, 5]); // 15
Get Intersection
import { getIntersection } from "regex-safe";
getIntersection([1, 2, 3], [2, 3, 4]); // [2, 3]
Get n-th Items
import { getNthItems } from "regex-safe";
getNthItems([1, 2, 3, 4, 5, 6, 7, 8, 9], 2); // [2, 4, 6, 8]
getNthItems([1, 2, 3, 4, 5, 6, 7, 8, 9], 3); // [3, 6, 9]
Get Consecutive Arrays
import { getConsecutiveArrays } from "regex-safe";
getConsecutiveArrays([1, 2, 3, 4, 5], 2); // [[1, 2], [2, 3], [3, 4], [4, 5]]
getConsecutiveArrays([1, 2, 3, 4, 5], 3); // [[1, 2, 3], [2, 3, 4], [3, 4, 5]]
getConsecutiveArrays([1, 2, 3, 4, 5], 6); // []
Flat an Array
import { flat } from "regex-safe";
flat(["regex-safe", ["regex", "safe"]]); // ["regex-safe", "regex", "safe"]
Index of Max
import { indexOfMax } from "regex-safe";
indexOfMax([6, 2, 5, 9, 7]); // 3
Index of Min
import { indexOfMin } from "regex-safe";
indexOfMin([6, 2, 5, 9, 7]); // 1
Max by Array
import { minByArray } from "regex-safe";
minByArray(
[
{
name: "John",
age: 20,
},
{
name: "Jane",
age: 30,
},
{
name: "Joe",
age: 10,
},
],
"age"
); // { name: "Joe", age: 10 },
Max by Array
import { maxByArray } from "regex-safe";
maxByArray(
[
{
name: "John",
age: 20,
},
{
name: "Jane",
age: 30,
},
{
name: "Joe",
age: 10,
},
],
"age"
); // { name: "Jane", age: 30 },
Range
import { range } from "regex-safe";
range(1, 10); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
range(2, 5); // [2, 3, 4, 5]
Convert Object
import { countOccurrences } from "regex-safe";
countOccurrences(["Mehmet", "Mehmet", "Regex", "Safe"], "mehmet"); // 2
countOccurrences([2, 1, 3, 4, 5, 2, 7, 2], 2); // 3
Convert Object
Convert to Object from Array.
import { convertArray } from "regex-safe";
convertObject(
[
{ id: 1, name: "John" },
{ id: 2, name: "Jane" },
],
"name"
); // { John: { id: 1, name: "John" }, Jane: { id: 2, name: "Jane" } }
convertObject([
{ id: 1, name: "John" },
{ id: 2, name: "Jane" },
]); // { 0: { id: 1, name: "John" }, 1: { id: 2, name: "Jane" } }
Cast Array
import { castArray } from "regex-safe";
castArray(1, 2, 3); // [1, 2, 3]
castArray([1, 2, 3]); // [1, 2, 3]
Equal Array
- Params
- First Array
- Second Array
- Type Sensitivity (Default: false)
import { isEqualArray } from "regex-safe";
isEqualArray([1, 2, 3], [1, "2", 3]); // true
isEqualArray([1, 2, 3], [1, "2", 3], true); // false
isEqualArray([1, 2, 3], [1, 2, 3], true); // true
Bold Text
- Params
- Text
- The text you want to be bold
- Case Sensitivity (Default: false)
import { boldText } from "regex-safe";
const text = "
You can make the areas you want bold in our texts using boldText. Example usage of BoldText is as follows.";
boldText(text, 'boldText'); // You can make the areas you want bold in our texts using <b>boldText</b>. Example usage of <b>BoldText</b> is as follows.
boldText(text, 'boldText', true); // You can make the areas you want bold in our texts using <b>boldText</b>. Example usage of BoldText is as follows.
Remove HTML Tags
import { removeHTMLTags } from "regex-safe";
removeHTMLTags("<h1>Hello world</h1>"); // Hello world
Use of Controls
import { isEmail } from "regex-safe";
isEmail("[email protected]"); // true
isEmail("[email protected]"); // false
URL
import { isURL } from "regex-safe";
isURL("https://mehmetsagir.com"); // true
isURL("htp://mehmetsagir.c"); // false
Number
import { isNum } from "regex-safe";
isNum("1"); // true
isNum("1a+"); // false
Name
Requires first and last name.
import { isName } from "regex-safe";
isName("Mehmet Sağır"); // true
isName("Mehmet"); // false
Password
By default all rules are enabled. English and Turkish error message options. Default language is English.
- Rules:
- atLeastOneNumber
- atLeastOneUpperCase
- atLeastOneLowerCase
- atLeastOneSpecialChar
- whitespace
- repeatedly
- minLength
import { isPassword } from "regex-safe";
isPassword("Test-123"); // true
isPassword("Test -123", { whitespace: false }, "tr"); // true
isPassword("Test -123"); // { valid: false, message: 'Must not contain whitespace.' }
isPassword("Test -123", {}, "tr"); // { valid: false, message: 'Boşluk içermemeli.' }
Image URL
URL and image types control.
import { isImageURL } from "regex-safe";
isImageURL("https://mehmetsagir.com/wallpaper/3.webp"); // true
isImageURL("https://mehmets>agir.com/wallpaper/3."); // false
External Regex
Allows us to check the value with External Regex.
import { isRegex } from "regex-safe";
isRegex("0123", /^[1-9]\d*$/g); // true
isRegex("Mehmet", /^[1-9]\d*$/g); // false
isRegex("Mehmet"); // false
Slug
import { isSlug } from "regex-safe";
isSlug("hello-world"); // true
isSlug("hello--world"); // false
Boolean
import { isBoolean } from "regex-safe";
isBoolean(false); // true
isBoolean("true"); // true
isBoolean("value"); // false
Even Number
import { isEven } from "regex-safe";
isEven(2); // true
isEven("2"); // true
isEven(1); // false
Odd Number
import { isOdd } from "regex-safe";
isOdd(3); // true
isOdd("3"); // true
isOdd(2); // false
Integer
import { isInteger } from "regex-safe";
isInteger(2); // true
isInteger(2.2); // false
Hex Color
import { isHexColor } from "regex-safe";
isHexColor("#000"); // true
isHexColor("00"); // false
IPv4 Address
import { isIPv4 } from "regex-safe";
isIPv4("01.102.103.104"); // true
isIPv4("01.102.103"); // false
IPv6 Address
import { isIPv6 } from "regex-safe";
isIPv6("2001:0db8:85a3:0000:0000:8a2e:0370:7334"); // true
isIPv6("2001:0db8:85a3:0000:0000:8a2e:0370"); // false
HTML Tags
import { isHTMLTags } from "regex-safe";
isHTMLTags("<h1>Mehmet</h1>"); // true
isHTMLTags("Mehmet"); // false
Versioning
We use GitHub for versioning.
Authors
License
Licensed under the MIT license, see LICENSE for details.