tsu 1.0.2 → 1.1.2

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.
package/src/scroll.ts DELETED
@@ -1,68 +0,0 @@
1
- import { isNumber, isString } from './is'
2
-
3
- type ScrollConfig = {
4
- to: Element | string | number
5
- offset?: number
6
- duration?: number
7
- container?: Element | string | null
8
- }
9
-
10
- /**
11
- * Scrolls the page or provided container to a target element or y-coordinate.
12
- *
13
- * @param config - The scroll config.
14
- * @param config.to - The scroll target.
15
- * @param config.offset - The offset from the scroll target (in px).
16
- * @param config.duration - The scroll duration (in ms).
17
- * @param config.container - The container to scroll in.
18
- */
19
- export function scroll({
20
- to,
21
- offset = 0,
22
- duration = 1000,
23
- container = null
24
- }: ScrollConfig): void {
25
- const target = isString(to) ? document.querySelector<HTMLElement>(to)! : to
26
- const _container = isString(container)
27
- ? document.querySelector<HTMLElement>(container)!
28
- : container
29
-
30
- const start = _container?.scrollTop || window?.pageYOffset || 0
31
- const end = (isNumber(target) ? target : getElTop(target, start)) + offset
32
-
33
- const startTime = Date.now()
34
-
35
- function step() {
36
- const timeElapsed = Date.now() - startTime
37
- const isScrolling = timeElapsed < duration
38
- const position = isScrolling
39
- ? getPosition(start, end, timeElapsed, duration)
40
- : end
41
-
42
- if (isScrolling) {
43
- requestAnimationFrame(step)
44
- }
45
-
46
- if (_container) {
47
- _container.scrollTop = position
48
- } else {
49
- window.scrollTo(0, position)
50
- }
51
- }
52
-
53
- step()
54
- }
55
-
56
- function easing(t: number): number {
57
- return t < 0.5 ? 4 * Math.pow(t, 3) : (t - 1) * (2 * t - 2) * (2 * t - 2) + 1
58
- }
59
-
60
- function getPosition(s: number, e: number, t: number, d: number): number {
61
- return s + (e - s) * easing(t / d)
62
- }
63
-
64
- function getElTop(element: Element, start: number): number {
65
- return element.nodeName === 'HTML'
66
- ? -start
67
- : element.getBoundingClientRect().top + start
68
- }
package/src/string.ts DELETED
@@ -1,103 +0,0 @@
1
- /**
2
- * Capitalises the first letter of one word, or all words, in a string.
3
- *
4
- * @param str - The string.
5
- * @param allWords - If all words should be capitalised.
6
- * @returns The capitalised string.
7
- */
8
- export function capitalise(str: string, allWords = false): string {
9
- if (!allWords) {
10
- return str.charAt(0).toUpperCase() + str.slice(1)
11
- }
12
-
13
- return str
14
- .split(' ')
15
- .map((s) => s.charAt(0).toUpperCase() + s.slice(1))
16
- .join(' ')
17
- }
18
-
19
- /**
20
- * Separates a string into an array of characters.
21
- *
22
- * @param str - The string.
23
- * @returns The string's characters.
24
- */
25
- export function chars(str: string): string[] {
26
- return str.split('')
27
- }
28
-
29
- /**
30
- * Identifies strings which only contain whitespace.
31
- *
32
- * @param str - The string.
33
- * @returns If the string is blank.
34
- */
35
- export function isBlank(str: string): boolean {
36
- return str.trim().length === 0
37
- }
38
-
39
- /**
40
- * Identifies empty strings.
41
- *
42
- * @param str - The string.
43
- * @returns If the string is empty.
44
- */
45
- export function isEmpty(str: string): boolean {
46
- return str.length === 0
47
- }
48
-
49
- /**
50
- * Converts a kebab case string to camel case.
51
- *
52
- * @param str - The kebab case string.
53
- * @returns The camel case string.
54
- */
55
- export function toCamel(str: string): string {
56
- const RE = /-(\w)/g
57
- return str.replace(RE, (_, c) => (c ? c.toUpperCase() : ''))
58
- }
59
-
60
- /**
61
- * Converts a camel case string to kebab case.
62
- *
63
- * @param str - The camel case string.
64
- * @returns The kebab case string.
65
- */
66
- export function toKebab(str: string): string {
67
- const RE = /\B([A-Z])/g
68
- return str.replace(RE, '-$1').toLowerCase()
69
- }
70
-
71
- /**
72
- * Converts a string which contains a number to the number itself.
73
- *
74
- * @param str - The string.
75
- * @returns The number.
76
- */
77
- export function toNumber(str: string): number {
78
- if (!str.length) {
79
- return NaN
80
- }
81
-
82
- return Number(str.replace(/[#£€$,%]/g, ''))
83
- }
84
-
85
- /**
86
- * Truncates a string to a provided length.
87
- *
88
- * @param str - The string.
89
- * @param length - The length.
90
- * @param truncateStr - The prefix to apply after truncation.
91
- * @returns The truncated string.
92
- */
93
- export function truncate(
94
- str: string,
95
- length: number,
96
- truncateStr = '...'
97
- ): string {
98
- if (length <= 0) {
99
- return truncateStr
100
- }
101
-
102
- return str.slice(0, length) + truncateStr
103
- }
package/src/types.ts DELETED
@@ -1,7 +0,0 @@
1
- export type Arrayable<T> = T | Array<T>
2
- export type Nullable<T> = T | null | undefined
3
-
4
- export type Fn<T = any> = (...args: any[]) => T
5
- export type VoidFn = Fn<void>
6
-
7
- export type Obj<V = unknown> = Record<string | symbol | number, V>