Skip to content

Commit

Permalink
Use TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
Schascha committed Oct 7, 2024
1 parent 2445202 commit e18e45e
Show file tree
Hide file tree
Showing 72 changed files with 1,514 additions and 219 deletions.
68 changes: 68 additions & 0 deletions dist/array.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* Finds a value in array recursively
* @param {any[]} array - The array to query
* @param {any|any[]} value - The value or array of values to find
* @return {boolean} Returns `true` if value is found, else `false`
*/
export declare function has(array: any[], value: any | any[]): boolean;
/**
* Checks if value or array is empty
* @param {any|any[]} value - The value or array to check
* @return {boolean} Returns `true` if value is empty, else `false`
*/
export declare function isEmpty(value: any | any[]): boolean;
/**
* Returns last element from array
* @param {any[]} array - The array to query
* @return {any} Returns the last element of the array or undefined
*/
export declare function last(array: any[]): any;
/**
* Returns next element from array
* @param {number} index - The index to query
* @param {any[]} array - The array to query
* @param {boolean} [loop=true] - Disable infinite array loop
* @returns {any} Return the previous element of the array
*/
export declare function next(index: number, array: any[], loop?: boolean): any;
/**
* Returns previous element from array
* @param {number} index - The index to query
* @param {any[]} array - The array to query
* @param {boolean} [loop=true] - Disable infinite array loop
* @returns {any} Returns the previous element of the array
*/
export declare function prev(index: number, array: any[], loop?: boolean): any;
/**
* Prints a value or an array of values
* @param {any|any[]} value - The value or array of values to print
* @param {string} [separator=', '] - The join separator
* @returns {string} Returns the value as a string
*/
export declare function print(value: any | any[], separator?: string): string;
/**
* Pushs a value or an array of values recursively
* @param {any[]} array - The array to add values to
* @param {any[]} value - The value or array of values to add
* @param {boolean} [unique=true] - If `true`, only unique values will be added
* @returns {number|undefined} Returns the size of the array or undefined
*/
export declare function push(array: any[], value: any[], unique?: boolean): number | undefined;
/**
* Returns random element from array
* @param {any[]} array - The array to query
* @return {any} Returns a random element or undefined
*/
export declare function random(array: any[]): any;
/**
* Randomize array
* @param {any[]} array - The array to be shuffled through
* @returns {any} Returns the shuffled array or undefined
*/
export declare function shuffle(array: any[]): any;
/**
* Returns value as an array if it's not one
* @param {any|any[]} value - The value to convert to an array
* @returns {any[]} Returns the value as an array
*/
export declare function toArray(value?: any | any[]): any[];
136 changes: 136 additions & 0 deletions dist/array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.has = has;
exports.isEmpty = isEmpty;
exports.last = last;
exports.next = next;
exports.prev = prev;
exports.print = print;
exports.push = push;
exports.random = random;
exports.shuffle = shuffle;
exports.toArray = toArray;
/**
* Finds a value in array recursively
* @param {any[]} array - The array to query
* @param {any|any[]} value - The value or array of values to find
* @return {boolean} Returns `true` if value is found, else `false`
*/
function has(array, value) {
if (!array || !value) {
return false;
}
array = toArray(array);
return Array.isArray(value)
? value.find((el) => has(array, el))
: array.includes(value);
}
/**
* Checks if value or array is empty
* @param {any|any[]} value - The value or array to check
* @return {boolean} Returns `true` if value is empty, else `false`
*/
function isEmpty(value) {
if (Array.isArray(value)) {
return !value.length || value.find((el) => isEmpty(el));
}
return value === null || value === '';
}
/**
* Returns last element from array
* @param {any[]} array - The array to query
* @return {any} Returns the last element of the array or undefined
*/
function last(array) {
if (Array.isArray(array)) {
return array[array.length - 1];
}
}
/**
* Returns next element from array
* @param {number} index - The index to query
* @param {any[]} array - The array to query
* @param {boolean} [loop=true] - Disable infinite array loop
* @returns {any} Return the previous element of the array
*/
function next(index, array, loop = true) {
if (Array.isArray(array)) {
const next = index + 1;
return array[loop ? next % array.length : next];
}
}
/**
* Returns previous element from array
* @param {number} index - The index to query
* @param {any[]} array - The array to query
* @param {boolean} [loop=true] - Disable infinite array loop
* @returns {any} Returns the previous element of the array
*/
function prev(index, array, loop = true) {
if (Array.isArray(array)) {
const length = array.length;
const prev = index - 1;
return array[loop ? (prev + length) % length : prev];
}
}
/**
* Prints a value or an array of values
* @param {any|any[]} value - The value or array of values to print
* @param {string} [separator=', '] - The join separator
* @returns {string} Returns the value as a string
*/
function print(value, separator = ', ') {
return toArray(value).join(separator);
}
/**
* Pushs a value or an array of values recursively
* @param {any[]} array - The array to add values to
* @param {any[]} value - The value or array of values to add
* @param {boolean} [unique=true] - If `true`, only unique values will be added
* @returns {number|undefined} Returns the size of the array or undefined
*/
function push(array, value, unique = true) {
if (!Array.isArray(array) || !value) {
return;
}
if (Array.isArray(value)) {
value.forEach((el) => push(array, el));
}
else {
if (!unique || (unique && !array.includes(value))) {
array.push(value);
}
}
return array.length;
}
/**
* Returns random element from array
* @param {any[]} array - The array to query
* @return {any} Returns a random element or undefined
*/
function random(array) {
if (Array.isArray(array)) {
return array[Math.floor(Math.random() * array.length)];
}
}
/**
* Randomize array
* @param {any[]} array - The array to be shuffled through
* @returns {any} Returns the shuffled array or undefined
*/
function shuffle(array) {
if (Array.isArray(array)) {
return array
.map((a) => [Math.random(), a])
.sort((a, b) => a[0] - b[0])
.map((a) => a[1]);
}
}
/**
* Returns value as an array if it's not one
* @param {any|any[]} value - The value to convert to an array
* @returns {any[]} Returns the value as an array
*/
function toArray(value = []) {
return Array.isArray(value) ? value : [value];
}
6 changes: 6 additions & 0 deletions dist/classnames.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* Conditional class name helper.
* @param {any} args Input of class names.
* @return {string} Returns `args` as string.
*/
export declare function classnames(...args: any): string;
26 changes: 26 additions & 0 deletions dist/classnames.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"use strict";
/**
* Conditional class name helper.
* @param {any} args Input of class names.
* @return {string} Returns `args` as string.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.classnames = classnames;
function classnames(...args) {
const classes = args.length > 1 ? args : args[0];
if (Array.isArray(classes)) {
return classes
.map((value) => classnames(value))
.join(' ')
.trim();
}
else if (classes &&
typeof classes === 'object' &&
classes.constructor === Object) {
return classnames(Object.keys(classes).filter((el) => {
const value = classes[el];
return Array.isArray(value) && !value.length ? false : value;
}));
}
return classes.trim();
}
26 changes: 26 additions & 0 deletions dist/cookie.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Get cookie
* @param {string} name - The name of the cookie
* @return {string|boolean} The value of the cookie or false if not found
*/
export declare function getCookie(name: string): string | boolean;
/**
* Set cookie
* @param {string} name - The name of the cookie
* @param {string} value - The value of the cookie
* @param {number} [days=1] - The number of days to keep the cookie
* @return {void}
*/
export declare function setCookie(name: string, value: string, days?: number): void;
/**
* Remove cookie
* @param {string} name - The name of the cookie
* @return {void}
*/
export declare function removeCookie(name: string): void;
/**
* Clear all cookies
* @param {string[]} [whitelist=[]] - The list of cookies to keep
* @return {void}
*/
export declare function clearCookies(whitelist?: string[]): void;
52 changes: 52 additions & 0 deletions dist/cookie.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getCookie = getCookie;
exports.setCookie = setCookie;
exports.removeCookie = removeCookie;
exports.clearCookies = clearCookies;
const date_1 = require("./date");
/**
* Get cookie
* @param {string} name - The name of the cookie
* @return {string|boolean} The value of the cookie or false if not found
*/
function getCookie(name) {
const match = document.cookie.match(new RegExp(`(^| )${name}=([^;]+)`));
return match ? match[2] : false;
}
/**
* Set cookie
* @param {string} name - The name of the cookie
* @param {string} value - The value of the cookie
* @param {number} [days=1] - The number of days to keep the cookie
* @return {void}
*/
function setCookie(name, value, days = 1) {
if (name) {
document.cookie = `${name}=${value}; expires=${(0, date_1.addDays)(new Date(), days).toUTCString()}; path=/;`;
}
}
/**
* Remove cookie
* @param {string} name - The name of the cookie
* @return {void}
*/
function removeCookie(name) {
setCookie(name, '', 0);
}
/**
* Clear all cookies
* @param {string[]} [whitelist=[]] - The list of cookies to keep
* @return {void}
*/
function clearCookies(whitelist = []) {
document.cookie.split(';').forEach((c) => {
const name = c.replace(/[=].*/, '').trim();
if (whitelist.includes(name)) {
return;
}
const domain = window.location.hostname.replace(/^([^.]+\.)/g, '.');
document.cookie = `${name}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;`;
document.cookie = `${name}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/; domain=${domain};`;
});
}
6 changes: 6 additions & 0 deletions dist/copy.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* Copy text to the clipboard
* @param {any} text - The text to copy
* @returns {void}
*/
export declare function copy(text: any): void;
21 changes: 21 additions & 0 deletions dist/copy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.copy = copy;
/**
* Copy text to the clipboard
* @param {any} text - The text to copy
* @returns {void}
*/
function copy(text) {
if (navigator && navigator.clipboard) {
navigator.clipboard.writeText(text);
}
else {
const input = document.createElement('input');
document.body.appendChild(input);
input.value = text;
input.select();
document.execCommand('copy');
document.body.removeChild(input);
}
}
41 changes: 41 additions & 0 deletions dist/date.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* Add days to a date
* @param {Date} date - The date to add days to
* @param {number} days - The number of days to add
* @returns {Date} The new date
*/
export declare function addDays(date: Date, days: number): Date;
/**
* Add months to a date
* @param {Date} date - The date to add months to
* @param {number} months - The number of months to add
* @returns {Date} The new date
*/
export declare function addMonths(date: Date, months: number): Date;
/**
* Add years to a date
* @param {Date} date - The date to add years to
* @param {number} years - The number of years to add
* @returns {Date} The new date
*/
export declare function addYears(date: Date, years: number): Date;
/**
* Get the difference in days between two dates
* @param {Date} date1 - The first date
* @param {Date} date2 - The second date
* @returns {number} The number of days between the two dates
*/
export declare function diffInDays(date1: Date, date2: Date): number;
/**
* Get the number of days in a month
* @param {Date} date - The date to get the number of days in the month for
* @returns {number} The number of days in the month
*/
export declare function daysInMonth(date: Date): number;
/**
* Format a date as YYYY-MM-DD or a custom format
* @param {Date} date - The date to format
* @param {string} format - The format to use, defaults to YYYY-MM-DD
* @returns {string} The formatted date as a string
*/
export declare function formatDate(date: Date, format?: string): string;
Loading

0 comments on commit e18e45e

Please sign in to comment.