From 33435548f01b4591aade321cc0d917f68c63be0b Mon Sep 17 00:00:00 2001 From: Ali-Eldeba Date: Sun, 19 Jun 2022 15:20:14 +0200 Subject: [PATCH 1/4] adding hextorgb and rgbtohex functions --- lib/main.d.ts | 4 ++++ lib/main.js | 4 ++++ lib/strings/hexToRgb.d.ts | 7 +++++++ lib/strings/hexToRgb.js | 13 +++++++++++++ lib/strings/rgbToHex.d.ts | 9 +++++++++ lib/strings/rgbToHex.js | 11 +++++++++++ project/ts/functionality/main.ts | 4 ++++ project/ts/functionality/strings/hexToRgb.ts | 16 ++++++++++++++++ project/ts/functionality/strings/rgbToHex.ts | 11 +++++++++++ 9 files changed, 79 insertions(+) create mode 100644 lib/strings/hexToRgb.d.ts create mode 100644 lib/strings/hexToRgb.js create mode 100644 lib/strings/rgbToHex.d.ts create mode 100644 lib/strings/rgbToHex.js create mode 100644 project/ts/functionality/strings/hexToRgb.ts create mode 100644 project/ts/functionality/strings/rgbToHex.ts diff --git a/lib/main.d.ts b/lib/main.d.ts index 80cdd75..23a64c6 100644 --- a/lib/main.d.ts +++ b/lib/main.d.ts @@ -22,6 +22,8 @@ import randomColor from "./randoms/randomColor"; import randomHsl from "./randoms/randomHsl"; import isHappyNumber from "./numbers/isHappyNumber"; import randomPassword from "./randoms/randomPassword"; +import rgbToHex from "./strings/rgbToHex"; +import hexToRgb from "./strings/hexToRgb"; declare const functionality: { sumOfArray: typeof sumOfArray; capitalize: typeof capitalize; @@ -47,6 +49,8 @@ declare const functionality: { randomHsl: typeof randomHsl; isHappyNumber: typeof isHappyNumber; randomPassword: typeof randomPassword; + rgbToHex: typeof rgbToHex; + hexToRgb: typeof hexToRgb; }; declare global { interface Window { diff --git a/lib/main.js b/lib/main.js index 76a682a..53f6d7a 100644 --- a/lib/main.js +++ b/lib/main.js @@ -22,6 +22,8 @@ import randomColor from "./randoms/randomColor"; import randomHsl from "./randoms/randomHsl"; import isHappyNumber from "./numbers/isHappyNumber"; import randomPassword from "./randoms/randomPassword"; +import rgbToHex from "./strings/rgbToHex"; +import hexToRgb from "./strings/hexToRgb"; const functionality = { sumOfArray, capitalize, @@ -47,6 +49,8 @@ const functionality = { randomHsl, isHappyNumber, randomPassword, + rgbToHex, + hexToRgb, }; window.functionality = functionality; // export * from "./sumOfArray"; diff --git a/lib/strings/hexToRgb.d.ts b/lib/strings/hexToRgb.d.ts new file mode 100644 index 0000000..f4bd8e3 --- /dev/null +++ b/lib/strings/hexToRgb.d.ts @@ -0,0 +1,7 @@ +/** + * converts a hex color to rgb + * + * @param {string} hex + * @returns {number[]} + */ +export default function hexToRgb(hex: string): number[]; diff --git a/lib/strings/hexToRgb.js b/lib/strings/hexToRgb.js new file mode 100644 index 0000000..b9be5a1 --- /dev/null +++ b/lib/strings/hexToRgb.js @@ -0,0 +1,13 @@ +/** + * converts a hex color to rgb + * + * @param {string} hex + * @returns {number[]} + */ +export default function hexToRgb(hex) { + return hex + .replace(/^#?([a-f\d])([a-f\d])([a-f\d])$/i, (m, r, g, b) => "#" + r + r + g + g + b + b) + .substring(1) + .match(/.{2}/g) + .map((x) => parseInt(x, 16)); +} diff --git a/lib/strings/rgbToHex.d.ts b/lib/strings/rgbToHex.d.ts new file mode 100644 index 0000000..ae1b12c --- /dev/null +++ b/lib/strings/rgbToHex.d.ts @@ -0,0 +1,9 @@ +/** + * Converts an RGB color value to a hexadecimal string. + * + * @param {number} r + * @param {number} g + * @param {number} b + * @returns {string} + */ +export default function rgbToHex(r: number, g: number, b: number): string; diff --git a/lib/strings/rgbToHex.js b/lib/strings/rgbToHex.js new file mode 100644 index 0000000..efe2c2a --- /dev/null +++ b/lib/strings/rgbToHex.js @@ -0,0 +1,11 @@ +/** + * Converts an RGB color value to a hexadecimal string. + * + * @param {number} r + * @param {number} g + * @param {number} b + * @returns {string} + */ +export default function rgbToHex(r, g, b) { + return "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1); +} diff --git a/project/ts/functionality/main.ts b/project/ts/functionality/main.ts index 7afee2c..7a89183 100644 --- a/project/ts/functionality/main.ts +++ b/project/ts/functionality/main.ts @@ -22,6 +22,8 @@ import randomColor from "./randoms/randomColor"; import randomHsl from "./randoms/randomHsl"; import isHappyNumber from "./numbers/isHappyNumber"; import randomPassword from "./randoms/randomPassword"; +import rgbToHex from "./strings/rgbToHex"; +import hexToRgb from "./strings/hexToRgb"; const functionality = { sumOfArray, @@ -48,6 +50,8 @@ const functionality = { randomHsl, isHappyNumber, randomPassword, + rgbToHex, + hexToRgb, }; declare global { diff --git a/project/ts/functionality/strings/hexToRgb.ts b/project/ts/functionality/strings/hexToRgb.ts new file mode 100644 index 0000000..2c85f42 --- /dev/null +++ b/project/ts/functionality/strings/hexToRgb.ts @@ -0,0 +1,16 @@ +/** + * converts a hex color to rgb + * + * @param {string} hex + * @returns {number[]} + */ +export default function hexToRgb(hex: string) { + return hex + .replace( + /^#?([a-f\d])([a-f\d])([a-f\d])$/i, + (m, r, g, b) => "#" + r + r + g + g + b + b + ) + .substring(1) + .match(/.{2}/g) + .map((x) => parseInt(x, 16)); +} diff --git a/project/ts/functionality/strings/rgbToHex.ts b/project/ts/functionality/strings/rgbToHex.ts new file mode 100644 index 0000000..ed8f897 --- /dev/null +++ b/project/ts/functionality/strings/rgbToHex.ts @@ -0,0 +1,11 @@ +/** + * Converts an RGB color value to a hexadecimal string. + * + * @param {number} r + * @param {number} g + * @param {number} b + * @returns {string} + */ +export default function rgbToHex(r: number, g: number, b: number): string { + return "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1); +} From fb1a348c18807d32c3dc103e1fe06407ced8ada0 Mon Sep 17 00:00:00 2001 From: Ali-Eldeba Date: Sun, 19 Jun 2022 16:34:36 +0200 Subject: [PATCH 2/4] adding some functions with errors --- project/ts/functionality/converts/hexToHsl.ts | 42 +++++++++++++++++++ .../{strings => converts}/hexToRgb.ts | 0 project/ts/functionality/converts/hslToHes.ts | 0 project/ts/functionality/converts/hslToRgb.ts | 0 .../{strings => converts}/rgbToHex.ts | 0 project/ts/functionality/converts/rgbToHsl.ts | 36 ++++++++++++++++ project/ts/functionality/main.ts | 6 ++- 7 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 project/ts/functionality/converts/hexToHsl.ts rename project/ts/functionality/{strings => converts}/hexToRgb.ts (100%) create mode 100644 project/ts/functionality/converts/hslToHes.ts create mode 100644 project/ts/functionality/converts/hslToRgb.ts rename project/ts/functionality/{strings => converts}/rgbToHex.ts (100%) create mode 100644 project/ts/functionality/converts/rgbToHsl.ts diff --git a/project/ts/functionality/converts/hexToHsl.ts b/project/ts/functionality/converts/hexToHsl.ts new file mode 100644 index 0000000..404e92d --- /dev/null +++ b/project/ts/functionality/converts/hexToHsl.ts @@ -0,0 +1,42 @@ +// Convert hex to RGB first +function hexToHsl(hex) { + // Convert hex to RGB first + let r, g, b; + if (hex.length === 4) { + r = "0x" + hex[1] + hex[1]; + g = "0x" + hex[2] + hex[2]; + b = "0x" + hex[3] + hex[3]; + } else if (hex.length === 7) { + r = "0x" + hex[1] + hex[2]; + g = "0x" + hex[3] + hex[4]; + b = "0x" + hex[5] + hex[6]; + } + // Then to HSL + r /= 255; + g /= 255; + b /= 255; + let cmin = Math.min(r, g, b), + cmax = Math.max(r, g, b), + delta = cmax - cmin, + h = 0, + s = 0, + l = 0; + + if (delta === 0) h = 0; + else if (cmax === r) h = ((g - b) / delta) % 6; + else if (cmax === g) h = (b - r) / delta + 2; + else h = (r - g) / delta + 4; + + h = Math.round(h * 60); + + if (h < 0) h += 360; + + l = (cmax + cmin) / 2; + s = delta === 0 ? 0 : delta / (1 - Math.abs(2 * l - 1)); + s = +(s * 100).toFixed(1); + l = +(l * 100).toFixed(1); + + console.log( + "hsl(" + Math.round(h) + "," + Math.round(s) + "," + Math.round(l) + ")" + ); +} diff --git a/project/ts/functionality/strings/hexToRgb.ts b/project/ts/functionality/converts/hexToRgb.ts similarity index 100% rename from project/ts/functionality/strings/hexToRgb.ts rename to project/ts/functionality/converts/hexToRgb.ts diff --git a/project/ts/functionality/converts/hslToHes.ts b/project/ts/functionality/converts/hslToHes.ts new file mode 100644 index 0000000..e69de29 diff --git a/project/ts/functionality/converts/hslToRgb.ts b/project/ts/functionality/converts/hslToRgb.ts new file mode 100644 index 0000000..e69de29 diff --git a/project/ts/functionality/strings/rgbToHex.ts b/project/ts/functionality/converts/rgbToHex.ts similarity index 100% rename from project/ts/functionality/strings/rgbToHex.ts rename to project/ts/functionality/converts/rgbToHex.ts diff --git a/project/ts/functionality/converts/rgbToHsl.ts b/project/ts/functionality/converts/rgbToHsl.ts new file mode 100644 index 0000000..8ece733 --- /dev/null +++ b/project/ts/functionality/converts/rgbToHsl.ts @@ -0,0 +1,36 @@ +/** + * + * @param r + * @param g + * @param b + * @returns + */ +export default function rgbToHsl(r: number, g: number, b: number): number[] { + (r /= 255), (g /= 255), (b /= 255); + var max = Math.max(r, g, b), + min = Math.min(r, g, b); + var h, + s, + l = (max + min) / 2; + + if (max == min) { + h = s = 0; + } else { + var d = max - min; + s = l > 0.5 ? d / (2 - max - min) : d / (max + min); + switch (max) { + case r: + h = (g - b) / d + (g < b ? 6 : 0); + break; + case g: + h = (b - r) / d + 2; + break; + case b: + h = (r - g) / d + 4; + break; + } + h /= 6; + } + + return [h, s, l]; +} diff --git a/project/ts/functionality/main.ts b/project/ts/functionality/main.ts index 7a89183..0e75b71 100644 --- a/project/ts/functionality/main.ts +++ b/project/ts/functionality/main.ts @@ -22,8 +22,9 @@ import randomColor from "./randoms/randomColor"; import randomHsl from "./randoms/randomHsl"; import isHappyNumber from "./numbers/isHappyNumber"; import randomPassword from "./randoms/randomPassword"; -import rgbToHex from "./strings/rgbToHex"; -import hexToRgb from "./strings/hexToRgb"; +import rgbToHex from "./converts/rgbToHex"; +import hexToRgb from "./converts/hexToRgb"; +import hexToHsl from "./converts/hexToHsl"; const functionality = { sumOfArray, @@ -52,6 +53,7 @@ const functionality = { randomPassword, rgbToHex, hexToRgb, + hexToHsl, }; declare global { From dcdaaa423721e7f07add47234ef2cfc6f1983b7f Mon Sep 17 00:00:00 2001 From: Ali-Eldeba Date: Mon, 20 Jun 2022 17:17:18 +0200 Subject: [PATCH 3/4] adding more than 10 functions --- project/ts/functionality/converts/hexToHsl.ts | 74 +++++++++---------- .../converts/{hslToHes.ts => hslToHex.ts} | 0 project/ts/functionality/dom/select.ts | 12 +++ project/ts/functionality/main.ts | 26 ++++++- .../functionality/strings/removeInnerSpace.ts | 13 ++++ project/ts/functionality/user/getBrowser.ts | 10 +++ project/ts/functionality/user/getDays.ts | 12 +++ project/ts/functionality/user/getHours.ts | 12 +++ project/ts/functionality/user/getMinutes.ts | 12 +++ project/ts/functionality/user/getMonths.ts | 12 +++ project/ts/functionality/user/getSeconds.ts | 12 +++ project/ts/functionality/user/getWeeks.ts | 12 +++ 12 files changed, 167 insertions(+), 40 deletions(-) rename project/ts/functionality/converts/{hslToHes.ts => hslToHex.ts} (100%) create mode 100644 project/ts/functionality/dom/select.ts create mode 100644 project/ts/functionality/strings/removeInnerSpace.ts create mode 100644 project/ts/functionality/user/getBrowser.ts create mode 100644 project/ts/functionality/user/getDays.ts create mode 100644 project/ts/functionality/user/getHours.ts create mode 100644 project/ts/functionality/user/getMinutes.ts create mode 100644 project/ts/functionality/user/getMonths.ts create mode 100644 project/ts/functionality/user/getSeconds.ts create mode 100644 project/ts/functionality/user/getWeeks.ts diff --git a/project/ts/functionality/converts/hexToHsl.ts b/project/ts/functionality/converts/hexToHsl.ts index 404e92d..3225887 100644 --- a/project/ts/functionality/converts/hexToHsl.ts +++ b/project/ts/functionality/converts/hexToHsl.ts @@ -1,42 +1,42 @@ -// Convert hex to RGB first -function hexToHsl(hex) { - // Convert hex to RGB first - let r, g, b; - if (hex.length === 4) { - r = "0x" + hex[1] + hex[1]; - g = "0x" + hex[2] + hex[2]; - b = "0x" + hex[3] + hex[3]; - } else if (hex.length === 7) { - r = "0x" + hex[1] + hex[2]; - g = "0x" + hex[3] + hex[4]; - b = "0x" + hex[5] + hex[6]; - } - // Then to HSL - r /= 255; - g /= 255; - b /= 255; - let cmin = Math.min(r, g, b), - cmax = Math.max(r, g, b), - delta = cmax - cmin, - h = 0, - s = 0, - l = 0; +// // Convert hex to RGB first +// export default function hexToHsl(hex: string) { +// // Convert hex to RGB first +// let r, g, b; +// if (hex.length === 4) { +// r = "0x" + hex[1] + hex[1]; +// g = "0x" + hex[2] + hex[2]; +// b = "0x" + hex[3] + hex[3]; +// } else if (hex.length === 7) { +// r = "0x" + hex[1] + hex[2]; +// g = "0x" + hex[3] + hex[4]; +// b = "0x" + hex[5] + hex[6]; +// } +// // Then to HSL +// r /= 255; +// g /= 255; +// b /= 255; +// let cmin = Math.min(r, g, b), +// cmax = Math.max(r, g, b), +// delta = cmax - cmin, +// h = 0, +// s = 0, +// l = 0; - if (delta === 0) h = 0; - else if (cmax === r) h = ((g - b) / delta) % 6; - else if (cmax === g) h = (b - r) / delta + 2; - else h = (r - g) / delta + 4; +// if (delta === 0) h = 0; +// else if (cmax === r) h = ((g - b) / delta) % 6; +// else if (cmax === g) h = (b - r) / delta + 2; +// else h = (r - g) / delta + 4; - h = Math.round(h * 60); +// h = Math.round(h * 60); - if (h < 0) h += 360; +// if (h < 0) h += 360; - l = (cmax + cmin) / 2; - s = delta === 0 ? 0 : delta / (1 - Math.abs(2 * l - 1)); - s = +(s * 100).toFixed(1); - l = +(l * 100).toFixed(1); +// l = (cmax + cmin) / 2; +// s = delta === 0 ? 0 : delta / (1 - Math.abs(2 * l - 1)); +// s = +(s * 100).toFixed(1); +// l = +(l * 100).toFixed(1); - console.log( - "hsl(" + Math.round(h) + "," + Math.round(s) + "," + Math.round(l) + ")" - ); -} +// console.log( +// "hsl(" + Math.round(h) + "," + Math.round(s) + "," + Math.round(l) + ")" +// ); +// } diff --git a/project/ts/functionality/converts/hslToHes.ts b/project/ts/functionality/converts/hslToHex.ts similarity index 100% rename from project/ts/functionality/converts/hslToHes.ts rename to project/ts/functionality/converts/hslToHex.ts diff --git a/project/ts/functionality/dom/select.ts b/project/ts/functionality/dom/select.ts new file mode 100644 index 0000000..f2ac74c --- /dev/null +++ b/project/ts/functionality/dom/select.ts @@ -0,0 +1,12 @@ +/** + * returns the number of Seconds in a given age + * + * @param {string} element + * @returns {number} + */ +export default function select(element: string): any { + if (typeof element !== "string") { + throw new TypeError("Expected a string but got " + typeof element); + } + return document.querySelector(element); +} diff --git a/project/ts/functionality/main.ts b/project/ts/functionality/main.ts index 0e75b71..0537ab1 100644 --- a/project/ts/functionality/main.ts +++ b/project/ts/functionality/main.ts @@ -24,7 +24,19 @@ import isHappyNumber from "./numbers/isHappyNumber"; import randomPassword from "./randoms/randomPassword"; import rgbToHex from "./converts/rgbToHex"; import hexToRgb from "./converts/hexToRgb"; -import hexToHsl from "./converts/hexToHsl"; +import rgbToHsl from "./converts/rgbToHsl"; +// import hexToHsl from "./converts/hexToHsl"; +// import hslToHex from "./converts/hslToHex"; +// import hslToRgb from "./converts/hslToRgb"; +import removeInnerSpace from "./strings/removeInnerSpace"; +import getBrowser from "./user/getBrowser"; +import getMonths from "./user/getMonths"; +import getWeeks from "./user/getWeeks"; +import getDays from "./user/getDays"; +import getHours from "./user/getHours"; +import getMinutes from "./user/getMinutes"; +import getSeconds from "./user/getSeconds"; +import select from "./dom/select"; const functionality = { sumOfArray, @@ -53,7 +65,16 @@ const functionality = { randomPassword, rgbToHex, hexToRgb, - hexToHsl, + rgbToHsl, + removeInnerSpace, + getBrowser, + getMonths, + getWeeks, + getDays, + getHours, + getSeconds, + getMinutes, + select, }; declare global { @@ -62,5 +83,4 @@ declare global { } } window.functionality = functionality; -// export * from "./sumOfArray"; export default functionality; diff --git a/project/ts/functionality/strings/removeInnerSpace.ts b/project/ts/functionality/strings/removeInnerSpace.ts new file mode 100644 index 0000000..4c7c754 --- /dev/null +++ b/project/ts/functionality/strings/removeInnerSpace.ts @@ -0,0 +1,13 @@ +/** + * Removes the inner whitespace from a string + * e.g. " Hello World " -> "Hello World" + * + * @param {string} word + * @returns {string} + */ +export default function removeInnerSpace(word: string): string { + if (typeof word !== "string") { + throw new TypeError("Expected a string but got " + typeof word); + } + return word.replace(/\s+/g, " "); +} diff --git a/project/ts/functionality/user/getBrowser.ts b/project/ts/functionality/user/getBrowser.ts new file mode 100644 index 0000000..bc335e7 --- /dev/null +++ b/project/ts/functionality/user/getBrowser.ts @@ -0,0 +1,10 @@ +/** + * Returns the browser name of the active user. + * + * @returns {string} + */ +export default function getBrowser(): string { + return navigator.userAgent.split(" ")[ + navigator.userAgent.split(" ").length - 1 + ]; +} diff --git a/project/ts/functionality/user/getDays.ts b/project/ts/functionality/user/getDays.ts new file mode 100644 index 0000000..d3174dd --- /dev/null +++ b/project/ts/functionality/user/getDays.ts @@ -0,0 +1,12 @@ +/** + * returns the number of days in a given age + * + * @param {number} age + * @returns {number} + */ + export default function getDays(age: number): number { + if (typeof age !== "number") { + throw new TypeError("Expected a number but got " + typeof age); + } + return age * 12 * 4 * 7; +} \ No newline at end of file diff --git a/project/ts/functionality/user/getHours.ts b/project/ts/functionality/user/getHours.ts new file mode 100644 index 0000000..4e811a1 --- /dev/null +++ b/project/ts/functionality/user/getHours.ts @@ -0,0 +1,12 @@ +/** + * returns the number of hours in a given age + * + * @param {number} age + * @returns {number} + */ +export default function getHours(age: number): number { + if (typeof age !== "number") { + throw new TypeError("Expected a number but got " + typeof age); + } + return age * 12 * 7 * 4 * 24; +} diff --git a/project/ts/functionality/user/getMinutes.ts b/project/ts/functionality/user/getMinutes.ts new file mode 100644 index 0000000..b658d1b --- /dev/null +++ b/project/ts/functionality/user/getMinutes.ts @@ -0,0 +1,12 @@ +/** + * returns the number of minutes in a given age + * + * @param {number} age + * @returns {number} + */ +export default function getWeeks(age: number): number { + if (typeof age !== "number") { + throw new TypeError("Expected a number but got " + typeof age); + } + return age * 12 * 7 * 4 * 24 * 60; +} diff --git a/project/ts/functionality/user/getMonths.ts b/project/ts/functionality/user/getMonths.ts new file mode 100644 index 0000000..5b816ab --- /dev/null +++ b/project/ts/functionality/user/getMonths.ts @@ -0,0 +1,12 @@ +/** + * returns the months of the age + * + * @param {number} age + * @returns {number} + */ +export default function getMonths(age: number): number { + if (typeof age !== "number") { + throw new TypeError("Expected a number but got " + typeof age); + } + return age * 12; +} diff --git a/project/ts/functionality/user/getSeconds.ts b/project/ts/functionality/user/getSeconds.ts new file mode 100644 index 0000000..a476c4c --- /dev/null +++ b/project/ts/functionality/user/getSeconds.ts @@ -0,0 +1,12 @@ +/** + * returns the number of Seconds in a given age + * + * @param {number} age + * @returns {number} + */ + export default function getSeconds(age: number): number { + if (typeof age !== "number") { + throw new TypeError("Expected a number but got " + typeof age); + } + return age * 12 * 7 * 4 * 24 * 60 * 60; +} \ No newline at end of file diff --git a/project/ts/functionality/user/getWeeks.ts b/project/ts/functionality/user/getWeeks.ts new file mode 100644 index 0000000..8478812 --- /dev/null +++ b/project/ts/functionality/user/getWeeks.ts @@ -0,0 +1,12 @@ +/** + * returns the number of weeks in a given age + * + * @param {number} age + * @returns {number} + */ +export default function getWeeks(age: number): number { + if (typeof age !== "number") { + throw new TypeError("Expected a number but got " + typeof age); + } + return age * 12 * 4; +} From 79f4f5a857c6c41135c8da2b322cc84f6675113a Mon Sep 17 00:00:00 2001 From: Ali-Eldeba Date: Mon, 20 Jun 2022 17:22:03 +0200 Subject: [PATCH 4/4] adding functions build --- lib/converts/hexToRgb.d.ts | 7 +++++++ lib/converts/hexToRgb.js | 13 +++++++++++++ lib/converts/rgbToHex.d.ts | 9 +++++++++ lib/converts/rgbToHex.js | 11 +++++++++++ lib/converts/rgbToHsl.d.ts | 8 ++++++++ lib/converts/rgbToHsl.js | 32 +++++++++++++++++++++++++++++++ lib/dom/select.d.ts | 7 +++++++ lib/dom/select.js | 12 ++++++++++++ lib/main.d.ts | 24 +++++++++++++++++++++-- lib/main.js | 28 ++++++++++++++++++++++++--- lib/strings/removeInnerSpace.d.ts | 8 ++++++++ lib/strings/removeInnerSpace.js | 13 +++++++++++++ lib/user/getBrowser.d.ts | 6 ++++++ lib/user/getBrowser.js | 8 ++++++++ lib/user/getDays.d.ts | 7 +++++++ lib/user/getDays.js | 12 ++++++++++++ lib/user/getHours.d.ts | 7 +++++++ lib/user/getHours.js | 12 ++++++++++++ lib/user/getMinutes.d.ts | 7 +++++++ lib/user/getMinutes.js | 12 ++++++++++++ lib/user/getMonths.d.ts | 7 +++++++ lib/user/getMonths.js | 12 ++++++++++++ lib/user/getSeconds.d.ts | 7 +++++++ lib/user/getSeconds.js | 12 ++++++++++++ lib/user/getWeeks.d.ts | 7 +++++++ lib/user/getWeeks.js | 12 ++++++++++++ 26 files changed, 295 insertions(+), 5 deletions(-) create mode 100644 lib/converts/hexToRgb.d.ts create mode 100644 lib/converts/hexToRgb.js create mode 100644 lib/converts/rgbToHex.d.ts create mode 100644 lib/converts/rgbToHex.js create mode 100644 lib/converts/rgbToHsl.d.ts create mode 100644 lib/converts/rgbToHsl.js create mode 100644 lib/dom/select.d.ts create mode 100644 lib/dom/select.js create mode 100644 lib/strings/removeInnerSpace.d.ts create mode 100644 lib/strings/removeInnerSpace.js create mode 100644 lib/user/getBrowser.d.ts create mode 100644 lib/user/getBrowser.js create mode 100644 lib/user/getDays.d.ts create mode 100644 lib/user/getDays.js create mode 100644 lib/user/getHours.d.ts create mode 100644 lib/user/getHours.js create mode 100644 lib/user/getMinutes.d.ts create mode 100644 lib/user/getMinutes.js create mode 100644 lib/user/getMonths.d.ts create mode 100644 lib/user/getMonths.js create mode 100644 lib/user/getSeconds.d.ts create mode 100644 lib/user/getSeconds.js create mode 100644 lib/user/getWeeks.d.ts create mode 100644 lib/user/getWeeks.js diff --git a/lib/converts/hexToRgb.d.ts b/lib/converts/hexToRgb.d.ts new file mode 100644 index 0000000..f4bd8e3 --- /dev/null +++ b/lib/converts/hexToRgb.d.ts @@ -0,0 +1,7 @@ +/** + * converts a hex color to rgb + * + * @param {string} hex + * @returns {number[]} + */ +export default function hexToRgb(hex: string): number[]; diff --git a/lib/converts/hexToRgb.js b/lib/converts/hexToRgb.js new file mode 100644 index 0000000..b9be5a1 --- /dev/null +++ b/lib/converts/hexToRgb.js @@ -0,0 +1,13 @@ +/** + * converts a hex color to rgb + * + * @param {string} hex + * @returns {number[]} + */ +export default function hexToRgb(hex) { + return hex + .replace(/^#?([a-f\d])([a-f\d])([a-f\d])$/i, (m, r, g, b) => "#" + r + r + g + g + b + b) + .substring(1) + .match(/.{2}/g) + .map((x) => parseInt(x, 16)); +} diff --git a/lib/converts/rgbToHex.d.ts b/lib/converts/rgbToHex.d.ts new file mode 100644 index 0000000..ae1b12c --- /dev/null +++ b/lib/converts/rgbToHex.d.ts @@ -0,0 +1,9 @@ +/** + * Converts an RGB color value to a hexadecimal string. + * + * @param {number} r + * @param {number} g + * @param {number} b + * @returns {string} + */ +export default function rgbToHex(r: number, g: number, b: number): string; diff --git a/lib/converts/rgbToHex.js b/lib/converts/rgbToHex.js new file mode 100644 index 0000000..efe2c2a --- /dev/null +++ b/lib/converts/rgbToHex.js @@ -0,0 +1,11 @@ +/** + * Converts an RGB color value to a hexadecimal string. + * + * @param {number} r + * @param {number} g + * @param {number} b + * @returns {string} + */ +export default function rgbToHex(r, g, b) { + return "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1); +} diff --git a/lib/converts/rgbToHsl.d.ts b/lib/converts/rgbToHsl.d.ts new file mode 100644 index 0000000..cde8e96 --- /dev/null +++ b/lib/converts/rgbToHsl.d.ts @@ -0,0 +1,8 @@ +/** + * + * @param r + * @param g + * @param b + * @returns + */ +export default function rgbToHsl(r: number, g: number, b: number): number[]; diff --git a/lib/converts/rgbToHsl.js b/lib/converts/rgbToHsl.js new file mode 100644 index 0000000..cd339d5 --- /dev/null +++ b/lib/converts/rgbToHsl.js @@ -0,0 +1,32 @@ +/** + * + * @param r + * @param g + * @param b + * @returns + */ +export default function rgbToHsl(r, g, b) { + (r /= 255), (g /= 255), (b /= 255); + var max = Math.max(r, g, b), min = Math.min(r, g, b); + var h, s, l = (max + min) / 2; + if (max == min) { + h = s = 0; + } + else { + var d = max - min; + s = l > 0.5 ? d / (2 - max - min) : d / (max + min); + switch (max) { + case r: + h = (g - b) / d + (g < b ? 6 : 0); + break; + case g: + h = (b - r) / d + 2; + break; + case b: + h = (r - g) / d + 4; + break; + } + h /= 6; + } + return [h, s, l]; +} diff --git a/lib/dom/select.d.ts b/lib/dom/select.d.ts new file mode 100644 index 0000000..e3cf31a --- /dev/null +++ b/lib/dom/select.d.ts @@ -0,0 +1,7 @@ +/** + * returns the number of Seconds in a given age + * + * @param {string} element + * @returns {number} + */ +export default function select(element: string): any; diff --git a/lib/dom/select.js b/lib/dom/select.js new file mode 100644 index 0000000..7d2d82f --- /dev/null +++ b/lib/dom/select.js @@ -0,0 +1,12 @@ +/** + * returns the number of Seconds in a given age + * + * @param {string} element + * @returns {number} + */ +export default function select(element) { + if (typeof element !== "string") { + throw new TypeError("Expected a string but got " + typeof element); + } + return document.querySelector(element); +} diff --git a/lib/main.d.ts b/lib/main.d.ts index 23a64c6..5c297a6 100644 --- a/lib/main.d.ts +++ b/lib/main.d.ts @@ -22,8 +22,18 @@ import randomColor from "./randoms/randomColor"; import randomHsl from "./randoms/randomHsl"; import isHappyNumber from "./numbers/isHappyNumber"; import randomPassword from "./randoms/randomPassword"; -import rgbToHex from "./strings/rgbToHex"; -import hexToRgb from "./strings/hexToRgb"; +import rgbToHex from "./converts/rgbToHex"; +import hexToRgb from "./converts/hexToRgb"; +import rgbToHsl from "./converts/rgbToHsl"; +import removeInnerSpace from "./strings/removeInnerSpace"; +import getBrowser from "./user/getBrowser"; +import getMonths from "./user/getMonths"; +import getWeeks from "./user/getWeeks"; +import getDays from "./user/getDays"; +import getHours from "./user/getHours"; +import getMinutes from "./user/getMinutes"; +import getSeconds from "./user/getSeconds"; +import select from "./dom/select"; declare const functionality: { sumOfArray: typeof sumOfArray; capitalize: typeof capitalize; @@ -51,6 +61,16 @@ declare const functionality: { randomPassword: typeof randomPassword; rgbToHex: typeof rgbToHex; hexToRgb: typeof hexToRgb; + rgbToHsl: typeof rgbToHsl; + removeInnerSpace: typeof removeInnerSpace; + getBrowser: typeof getBrowser; + getMonths: typeof getMonths; + getWeeks: typeof getWeeks; + getDays: typeof getDays; + getHours: typeof getHours; + getSeconds: typeof getSeconds; + getMinutes: typeof getMinutes; + select: typeof select; }; declare global { interface Window { diff --git a/lib/main.js b/lib/main.js index 53f6d7a..fbfbc67 100644 --- a/lib/main.js +++ b/lib/main.js @@ -22,8 +22,21 @@ import randomColor from "./randoms/randomColor"; import randomHsl from "./randoms/randomHsl"; import isHappyNumber from "./numbers/isHappyNumber"; import randomPassword from "./randoms/randomPassword"; -import rgbToHex from "./strings/rgbToHex"; -import hexToRgb from "./strings/hexToRgb"; +import rgbToHex from "./converts/rgbToHex"; +import hexToRgb from "./converts/hexToRgb"; +import rgbToHsl from "./converts/rgbToHsl"; +// import hexToHsl from "./converts/hexToHsl"; +// import hslToHex from "./converts/hslToHex"; +// import hslToRgb from "./converts/hslToRgb"; +import removeInnerSpace from "./strings/removeInnerSpace"; +import getBrowser from "./user/getBrowser"; +import getMonths from "./user/getMonths"; +import getWeeks from "./user/getWeeks"; +import getDays from "./user/getDays"; +import getHours from "./user/getHours"; +import getMinutes from "./user/getMinutes"; +import getSeconds from "./user/getSeconds"; +import select from "./dom/select"; const functionality = { sumOfArray, capitalize, @@ -51,7 +64,16 @@ const functionality = { randomPassword, rgbToHex, hexToRgb, + rgbToHsl, + removeInnerSpace, + getBrowser, + getMonths, + getWeeks, + getDays, + getHours, + getSeconds, + getMinutes, + select, }; window.functionality = functionality; -// export * from "./sumOfArray"; export default functionality; diff --git a/lib/strings/removeInnerSpace.d.ts b/lib/strings/removeInnerSpace.d.ts new file mode 100644 index 0000000..f5dc2ee --- /dev/null +++ b/lib/strings/removeInnerSpace.d.ts @@ -0,0 +1,8 @@ +/** + * Removes the inner whitespace from a string + * e.g. " Hello World " -> "Hello World" + * + * @param {string} word + * @returns {string} + */ +export default function removeInnerSpace(word: string): string; diff --git a/lib/strings/removeInnerSpace.js b/lib/strings/removeInnerSpace.js new file mode 100644 index 0000000..f76e6a8 --- /dev/null +++ b/lib/strings/removeInnerSpace.js @@ -0,0 +1,13 @@ +/** + * Removes the inner whitespace from a string + * e.g. " Hello World " -> "Hello World" + * + * @param {string} word + * @returns {string} + */ +export default function removeInnerSpace(word) { + if (typeof word !== "string") { + throw new TypeError("Expected a string but got " + typeof word); + } + return word.replace(/\s+/g, " "); +} diff --git a/lib/user/getBrowser.d.ts b/lib/user/getBrowser.d.ts new file mode 100644 index 0000000..2041a84 --- /dev/null +++ b/lib/user/getBrowser.d.ts @@ -0,0 +1,6 @@ +/** + * Returns the browser name of the active user. + * + * @returns {string} + */ +export default function getBrowser(): string; diff --git a/lib/user/getBrowser.js b/lib/user/getBrowser.js new file mode 100644 index 0000000..d1397c3 --- /dev/null +++ b/lib/user/getBrowser.js @@ -0,0 +1,8 @@ +/** + * Returns the browser name of the active user. + * + * @returns {string} + */ +export default function getBrowser() { + return navigator.userAgent.split(" ")[navigator.userAgent.split(" ").length - 1]; +} diff --git a/lib/user/getDays.d.ts b/lib/user/getDays.d.ts new file mode 100644 index 0000000..97dc114 --- /dev/null +++ b/lib/user/getDays.d.ts @@ -0,0 +1,7 @@ +/** + * returns the number of days in a given age + * + * @param {number} age + * @returns {number} + */ +export default function getDays(age: number): number; diff --git a/lib/user/getDays.js b/lib/user/getDays.js new file mode 100644 index 0000000..e26bf56 --- /dev/null +++ b/lib/user/getDays.js @@ -0,0 +1,12 @@ +/** + * returns the number of days in a given age + * + * @param {number} age + * @returns {number} + */ +export default function getDays(age) { + if (typeof age !== "number") { + throw new TypeError("Expected a number but got " + typeof age); + } + return age * 12 * 4 * 7; +} diff --git a/lib/user/getHours.d.ts b/lib/user/getHours.d.ts new file mode 100644 index 0000000..fc2971e --- /dev/null +++ b/lib/user/getHours.d.ts @@ -0,0 +1,7 @@ +/** + * returns the number of hours in a given age + * + * @param {number} age + * @returns {number} + */ +export default function getHours(age: number): number; diff --git a/lib/user/getHours.js b/lib/user/getHours.js new file mode 100644 index 0000000..a15dd09 --- /dev/null +++ b/lib/user/getHours.js @@ -0,0 +1,12 @@ +/** + * returns the number of hours in a given age + * + * @param {number} age + * @returns {number} + */ +export default function getHours(age) { + if (typeof age !== "number") { + throw new TypeError("Expected a number but got " + typeof age); + } + return age * 12 * 7 * 4 * 24; +} diff --git a/lib/user/getMinutes.d.ts b/lib/user/getMinutes.d.ts new file mode 100644 index 0000000..72a13f5 --- /dev/null +++ b/lib/user/getMinutes.d.ts @@ -0,0 +1,7 @@ +/** + * returns the number of minutes in a given age + * + * @param {number} age + * @returns {number} + */ +export default function getWeeks(age: number): number; diff --git a/lib/user/getMinutes.js b/lib/user/getMinutes.js new file mode 100644 index 0000000..a6791b9 --- /dev/null +++ b/lib/user/getMinutes.js @@ -0,0 +1,12 @@ +/** + * returns the number of minutes in a given age + * + * @param {number} age + * @returns {number} + */ +export default function getWeeks(age) { + if (typeof age !== "number") { + throw new TypeError("Expected a number but got " + typeof age); + } + return age * 12 * 7 * 4 * 24 * 60; +} diff --git a/lib/user/getMonths.d.ts b/lib/user/getMonths.d.ts new file mode 100644 index 0000000..bccd283 --- /dev/null +++ b/lib/user/getMonths.d.ts @@ -0,0 +1,7 @@ +/** + * returns the months of the age + * + * @param {number} age + * @returns {number} + */ +export default function getMonths(age: number): number; diff --git a/lib/user/getMonths.js b/lib/user/getMonths.js new file mode 100644 index 0000000..b32e02d --- /dev/null +++ b/lib/user/getMonths.js @@ -0,0 +1,12 @@ +/** + * returns the months of the age + * + * @param {number} age + * @returns {number} + */ +export default function getMonths(age) { + if (typeof age !== "number") { + throw new TypeError("Expected a number but got " + typeof age); + } + return age * 12; +} diff --git a/lib/user/getSeconds.d.ts b/lib/user/getSeconds.d.ts new file mode 100644 index 0000000..13b6b19 --- /dev/null +++ b/lib/user/getSeconds.d.ts @@ -0,0 +1,7 @@ +/** + * returns the number of Seconds in a given age + * + * @param {number} age + * @returns {number} + */ +export default function getSeconds(age: number): number; diff --git a/lib/user/getSeconds.js b/lib/user/getSeconds.js new file mode 100644 index 0000000..920fb17 --- /dev/null +++ b/lib/user/getSeconds.js @@ -0,0 +1,12 @@ +/** + * returns the number of Seconds in a given age + * + * @param {number} age + * @returns {number} + */ +export default function getSeconds(age) { + if (typeof age !== "number") { + throw new TypeError("Expected a number but got " + typeof age); + } + return age * 12 * 7 * 4 * 24 * 60 * 60; +} diff --git a/lib/user/getWeeks.d.ts b/lib/user/getWeeks.d.ts new file mode 100644 index 0000000..7a79fdb --- /dev/null +++ b/lib/user/getWeeks.d.ts @@ -0,0 +1,7 @@ +/** + * returns the number of weeks in a given age + * + * @param {number} age + * @returns {number} + */ +export default function getWeeks(age: number): number; diff --git a/lib/user/getWeeks.js b/lib/user/getWeeks.js new file mode 100644 index 0000000..fe4ae3b --- /dev/null +++ b/lib/user/getWeeks.js @@ -0,0 +1,12 @@ +/** + * returns the number of weeks in a given age + * + * @param {number} age + * @returns {number} + */ +export default function getWeeks(age) { + if (typeof age !== "number") { + throw new TypeError("Expected a number but got " + typeof age); + } + return age * 12 * 4; +}