-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add event modifiers and string utils
- Loading branch information
1 parent
08187bd
commit f10dbef
Showing
6 changed files
with
90 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()}`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
} |