Skip to content

Commit

Permalink
Add event modifiers and string utils
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaitanLyss committed Jul 12, 2024
1 parent 08187bd commit f10dbef
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 3 deletions.
Binary file modified bun.lockb
Binary file not shown.
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@selenite/commons",
"version": "0.0.6",
"version": "0.1.1",
"scripts": {
"dev": "vite dev",
"build": "vite build && npm run package",
Expand Down Expand Up @@ -54,5 +54,9 @@
},
"svelte": "./dist/index.js",
"types": "./dist/index.d.ts",
"type": "module"
"type": "module",
"dependencies": {
"@types/uuid": "^10.0.0",
"uuid": "^10.0.0"
}
}
3 changes: 2 additions & 1 deletion src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
// Reexport your entry components here
export * from './actions/index.js'
export * from './actions/index.js'
export * from './utils/index.js'
11 changes: 11 additions & 0 deletions src/lib/utils/eventListeners.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export type EventModifier<E extends Event = Event> = (e: E) => E

export const stopPropagation: EventModifier = (e) => {
e.stopPropagation();
return e
}

export const preventDefault: EventModifier = (e) => {
e.preventDefault();
return e
}
17 changes: 17 additions & 0 deletions src/lib/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// export * from './uuid';
// export * from './context';
// export { formatXml } from './xml.js';
export * from './string.js';
export * from './eventListeners.js';

export { v4 as uuidv4 } from 'uuid';
import {v4 as uuid} from 'uuid'
let idCount = 0;
export function newLocalId(baseName?: string) {
idCount += 1;
return `${baseName ?? 'local-unique-id'}-${idCount}}`;
}

export function newUuid(baseName?: string) {
return `${baseName ? baseName + '-' : ''}${uuid()}`;
}
54 changes: 54 additions & 0 deletions src/lib/utils/string.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
export function capitalize(str: string): string {
return str.charAt(0).toUpperCase() + str.slice(1);
}

export function capitalizeWords(str: string): string {
return str.split(' ').map(capitalize).join(' ');
}

export function isAlphaNumChar(str: string) {
return /^[a-z0-9]$/i.test(str);
}

const identity = (str: string) => str;
export const words = capitalizeWords;

// split on capital letters that are followed by non capital letters or
// preceded by non capital letters
// and join with spaces
export function titlelize(str: string): string {
return str
.split(/(?=(?<=[^A-Z])[A-Z])/)

.map(capitalize)
.join(' ');
}

// returns the first two capital letters of a string or the first two letters if there is not enough capital letters
export function initials(str: string): string {
str = capitalizeWords(str);
const capitalLetters = str.match(/^([A-Z])(?:\S*?\s+|\S*?)([A-Z])/);
if (capitalLetters == null) {
return str.slice(0, 2);
}

return capitalLetters[1] + capitalLetters[2];
}

// export function titlelize(str: string): string {
// return str
// .split(/(?=[A-Z])/)
// .map(capitalize)
// .join(' ');
// }

// remove spaces and capitalize all but first letter
export function camlelcaseize(str: string): string {
const capitalized = str.split(' ').map(capitalize).join('');
return capitalized.charAt(0).toLowerCase() + capitalized.slice(1);
}

export function getVarsFromFormatString(formatString: string): string[] {
// return all matches of the regex
return Array.from(formatString.matchAll(/{(\w+).*?}/g)).map((match) => match[1]);
}

0 comments on commit f10dbef

Please sign in to comment.