@citruslime/utils 0.0.1-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,107 @@
1
+ function generateStringId() {
2
+ return Math.random().toString(36).slice(2, 8);
3
+ }
4
+ function nameOf(name) {
5
+ return name;
6
+ }
7
+ function compareByProperty(firstValue, secondValue, property) {
8
+ return firstValue[property] < secondValue[property] ? -1 : firstValue[property] > secondValue[property] ? 1 : 0;
9
+ }
10
+ function getParamsByName(stringToSearch, paramToFind) {
11
+ let valueToReturn = "";
12
+ const urlToSearch = decodeURIComponent(stringToSearch);
13
+ const regexS = "[\\?&]" + paramToFind + "=([^&#]*)";
14
+ const regex = new RegExp(regexS);
15
+ const results = regex.exec(urlToSearch);
16
+ if (results && results[1]) {
17
+ valueToReturn = decodeURIComponent(results[1].replace(/\+/g, " "));
18
+ }
19
+ return valueToReturn;
20
+ }
21
+ String.prototype.similarity = function(comparisonString) {
22
+ let similarityToReturn = 0;
23
+ let longerString = this.toLowerCase();
24
+ let shorterString = comparisonString.toLowerCase();
25
+ if (longerString < shorterString) {
26
+ const temp = shorterString;
27
+ shorterString = longerString;
28
+ longerString = temp;
29
+ }
30
+ const longerLength = longerString.length;
31
+ if (longerLength === 0) {
32
+ similarityToReturn = 100;
33
+ } else {
34
+ similarityToReturn = Math.round((longerLength - levenshtein(longerString, shorterString)) / longerLength * 100);
35
+ }
36
+ return similarityToReturn;
37
+ };
38
+ String.prototype.trimChar = function(character) {
39
+ return this.trimStartChar(character).trimEndChar(character);
40
+ };
41
+ String.prototype.trimStartChar = function(character) {
42
+ return character !== "" && this.startsWith(character) ? this.slice(1) : this;
43
+ };
44
+ String.prototype.trimEndChar = function(character) {
45
+ return character !== "" && this.endsWith(character) ? this.slice(0, -1) : this;
46
+ };
47
+ String.prototype.trimToLength = function(length, addEllipsis = false) {
48
+ const trimLength = this.length < length ? this.length : length;
49
+ return addEllipsis && trimLength >= 4 && trimLength < this.length ? `${this.slice(0, Math.max(trimLength - 3, 1))}...` : this.slice(0, Math.max(trimLength, 1));
50
+ };
51
+ String.prototype.removeWhitespace = function() {
52
+ return this.replace(/\s/g, "");
53
+ };
54
+ String.prototype.removeNonAlphanumeric = function() {
55
+ return this.replace(/[^a-zA-Z0-9]/g, "");
56
+ };
57
+ String.prototype.removeNonAlphabetic = function() {
58
+ return this.replace(/[^a-zA-Z]/g, "");
59
+ };
60
+ String.prototype.removeNonNumeric = function() {
61
+ return this.replace(/[^0-9]/g, "");
62
+ };
63
+ String.prototype.replacePlaceholders = function(...args) {
64
+ let i = 0;
65
+ return this.replace(/{\d+}/g, () => typeof args[i] !== "undefined" ? args[i++] : "");
66
+ };
67
+ String.prototype.toDateFromTime = function() {
68
+ return new Date(`${new Date().toISOString().substr(0, 11)}${this}`);
69
+ };
70
+ function levenshtein(firstString, secondString) {
71
+ if (firstString.length === 0) {
72
+ return secondString.length;
73
+ }
74
+ if (secondString.length === 0) {
75
+ return firstString.length;
76
+ }
77
+ if (firstString.length > secondString.length) {
78
+ const tmp = firstString;
79
+ firstString = secondString;
80
+ secondString = tmp;
81
+ }
82
+ const row = new Int8Array(firstString.length + 1);
83
+ for (let i = 0; i <= firstString.length; i++) {
84
+ row[i] = i;
85
+ }
86
+ let temp;
87
+ for (let i = 1; i <= secondString.length; i++) {
88
+ let prev = i;
89
+ const previousChar = secondString[i - 1];
90
+ for (let j = 1; j <= firstString.length; j++) {
91
+ if (previousChar === firstString[j - 1]) {
92
+ temp = row[j - 1];
93
+ } else {
94
+ const a = prev + 1;
95
+ const b = row[j] + 1;
96
+ const c = a - (a - b & b - a >> 7);
97
+ const d = row[j - 1] + 1;
98
+ temp = c - (c - d & d - c >> 7);
99
+ }
100
+ row[j - 1] = prev;
101
+ prev = temp;
102
+ }
103
+ row[firstString.length] = prev;
104
+ }
105
+ return row[firstString.length];
106
+ }
107
+ export { compareByProperty, generateStringId, getParamsByName, nameOf };
@@ -0,0 +1 @@
1
+ (function(o,u){typeof exports=="object"&&typeof module!="undefined"?u(exports):typeof define=="function"&&define.amd?define(["exports"],u):(o=typeof globalThis!="undefined"?globalThis:o||self,u(o.CitrusLimeUtils={}))})(this,function(o){"use strict";function u(){return Math.random().toString(36).slice(2,8)}function g(e){return e}function f(e,n,t){return e[t]<n[t]?-1:e[t]>n[t]?1:0}function y(e,n){let t="";const i=decodeURIComponent(e),r="[\\?&]"+n+"=([^&#]*)",s=new RegExp(r).exec(i);return s&&s[1]&&(t=decodeURIComponent(s[1].replace(/\+/g," "))),t}String.prototype.similarity=function(e){let n=0,t=this.toLowerCase(),i=e.toLowerCase();if(t<i){const l=i;i=t,t=l}const r=t.length;return r===0?n=100:n=Math.round((r-d(t,i))/r*100),n},String.prototype.trimChar=function(e){return this.trimStartChar(e).trimEndChar(e)},String.prototype.trimStartChar=function(e){return e!==""&&this.startsWith(e)?this.slice(1):this},String.prototype.trimEndChar=function(e){return e!==""&&this.endsWith(e)?this.slice(0,-1):this},String.prototype.trimToLength=function(e,n=!1){const t=this.length<e?this.length:e;return n&&t>=4&&t<this.length?`${this.slice(0,Math.max(t-3,1))}...`:this.slice(0,Math.max(t,1))},String.prototype.removeWhitespace=function(){return this.replace(/\s/g,"")},String.prototype.removeNonAlphanumeric=function(){return this.replace(/[^a-zA-Z0-9]/g,"")},String.prototype.removeNonAlphabetic=function(){return this.replace(/[^a-zA-Z]/g,"")},String.prototype.removeNonNumeric=function(){return this.replace(/[^0-9]/g,"")},String.prototype.replacePlaceholders=function(...e){let n=0;return this.replace(/{\d+}/g,()=>typeof e[n]!="undefined"?e[n++]:"")},String.prototype.toDateFromTime=function(){return new Date(`${new Date().toISOString().substr(0,11)}${this}`)};function d(e,n){if(e.length===0)return n.length;if(n.length===0)return e.length;if(e.length>n.length){const r=e;e=n,n=r}const t=new Int8Array(e.length+1);for(let r=0;r<=e.length;r++)t[r]=r;let i;for(let r=1;r<=n.length;r++){let l=r;const s=n[r-1];for(let h=1;h<=e.length;h++){if(s===e[h-1])i=t[h-1];else{const a=l+1,p=t[h]+1,c=a-(a-p&p-a>>7),m=t[h-1]+1;i=c-(c-m&m-c>>7)}t[h-1]=l,l=i}t[e.length]=l}return t[e.length]}o.compareByProperty=f,o.generateStringId=u,o.getParamsByName=y,o.nameOf=g,Object.defineProperty(o,"__esModule",{value:!0}),o[Symbol.toStringTag]="Module"});
package/dist/main.d.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './utils';
@@ -0,0 +1,18 @@
1
+ declare global {
2
+ interface String {
3
+ similarity(comparisonString: string): number;
4
+ trimChar(character: string): string;
5
+ trimStartChar(character: string): string;
6
+ trimEndChar(character: string): string;
7
+ trimToLength(length: number, addEllipsis?: boolean): string;
8
+ firstCharToUpperCase(locale?: string | string[]): string;
9
+ firstCharToLowerCase(locale?: string | string[]): string;
10
+ removeWhitespace(): string;
11
+ removeNonAlphanumeric(): string;
12
+ removeNonAlphabetic(): string;
13
+ removeNonNumeric(): string;
14
+ replacePlaceholders(...args: string[]): string;
15
+ toDateFromTime(): Date;
16
+ }
17
+ }
18
+ export {} from './prototypes';
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Generates a random string ID.
3
+ *
4
+ * @returns The created string ID.
5
+ */
6
+ export declare function generateStringId(): string;
@@ -0,0 +1,5 @@
1
+ export * from './id';
2
+ export * from './name-of';
3
+ export * from './sort';
4
+ export * from './url-params';
5
+ export {} from './extensions/string';
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Wrapper to safely guarantee a string is the name of a property of an object.
3
+ *
4
+ * @param name The key of the provided type.
5
+ * @returns The provided key, if it belongs to the specified type.
6
+ */
7
+ export declare function nameOf<T>(name: Extract<keyof T, string>): keyof T;
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Compares the two provided objects by the value of their specified property.
3
+ *
4
+ * @param firstValue The first object to compare.
5
+ * @param secondValue The second object to compare.
6
+ * @param property The property, of the two objects, to compare.
7
+ * @returns 0, if equal; 1, if the first is greater; -1, if the second is greater.
8
+ */
9
+ export declare function compareByProperty<T>(firstValue: T, secondValue: T, property: keyof T): -1 | 0 | 1;
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Searches the provided URL to for a specific parameter; returns it, if found.
3
+ *
4
+ * @param stringToSearch Encoded URL to search in.
5
+ * @param paramToFind Parameter to find.
6
+ * @returns The param, if found, or an empty string.
7
+ */
8
+ export declare function getParamsByName(stringToSearch: string, paramToFind: string): string;
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@citruslime/utils",
3
+ "version": "0.0.1-beta.1",
4
+ "author": {
5
+ "name": "Citrus-Lime Ltd",
6
+ "url": "https://citruslime.com"
7
+ },
8
+ "license": "MIT",
9
+ "homepage": "https://uilibrary.citruslime.com/",
10
+ "publishConfig": {
11
+ "access": "public"
12
+ },
13
+ "files": [
14
+ "dist"
15
+ ],
16
+ "main": "dist/citrus-lime-utils.umd.js",
17
+ "module": "dist/citrus-lime-utils.es.js",
18
+ "types": "dist/main.d.ts",
19
+ "exports": {
20
+ ".": {
21
+ "import": "./dist/citrus-lime-utils.es.js",
22
+ "require": "./dist/citrus-lime-utils.umd.js"
23
+ }
24
+ },
25
+ "scripts": {
26
+ "build": "vite build && copyfiles -u 2 \"dist/src/**/*\" dist && del-cli dist/src"
27
+ },
28
+ "devDependencies": {
29
+ "copyfiles": "^2.4.1",
30
+ "del-cli": "^4.0.1",
31
+ "vite-plugin-dts": "^0.9.4"
32
+ }
33
+ }