aboutsummaryrefslogtreecommitdiffhomepage
path: root/packages/core-utils
diff options
context:
space:
mode:
Diffstat (limited to 'packages/core-utils')
-rw-r--r--packages/core-utils/src/common/array.ts22
-rw-r--r--packages/core-utils/src/common/date.ts4
-rw-r--r--packages/core-utils/src/index.ts1
-rw-r--r--packages/core-utils/src/string/chapters.ts32
-rw-r--r--packages/core-utils/src/string/index.ts1
5 files changed, 50 insertions, 10 deletions
diff --git a/packages/core-utils/src/common/array.ts b/packages/core-utils/src/common/array.ts
index 878ed1ffe..3978ddd16 100644
--- a/packages/core-utils/src/common/array.ts
+++ b/packages/core-utils/src/common/array.ts
@@ -1,4 +1,4 @@
1function findCommonElement <T> (array1: T[], array2: T[]) { 1export function findCommonElement <T> (array1: T[], array2: T[]) {
2 for (const a of array1) { 2 for (const a of array1) {
3 for (const b of array2) { 3 for (const b of array2) {
4 if (a === b) return a 4 if (a === b) return a
@@ -9,19 +9,19 @@ function findCommonElement <T> (array1: T[], array2: T[]) {
9} 9}
10 10
11// Avoid conflict with other toArray() functions 11// Avoid conflict with other toArray() functions
12function arrayify <T> (element: T | T[]) { 12export function arrayify <T> (element: T | T[]) {
13 if (Array.isArray(element)) return element 13 if (Array.isArray(element)) return element
14 14
15 return [ element ] 15 return [ element ]
16} 16}
17 17
18// Avoid conflict with other uniq() functions 18// Avoid conflict with other uniq() functions
19function uniqify <T> (elements: T[]) { 19export function uniqify <T> (elements: T[]) {
20 return Array.from(new Set(elements)) 20 return Array.from(new Set(elements))
21} 21}
22 22
23// Thanks: https://stackoverflow.com/a/12646864 23// Thanks: https://stackoverflow.com/a/12646864
24function shuffle <T> (elements: T[]) { 24export function shuffle <T> (elements: T[]) {
25 const shuffled = [ ...elements ] 25 const shuffled = [ ...elements ]
26 26
27 for (let i = shuffled.length - 1; i > 0; i--) { 27 for (let i = shuffled.length - 1; i > 0; i--) {
@@ -33,9 +33,13 @@ function shuffle <T> (elements: T[]) {
33 return shuffled 33 return shuffled
34} 34}
35 35
36export { 36export function sortBy (obj: any[], key1: string, key2?: string) {
37 uniqify, 37 return obj.sort((a, b) => {
38 findCommonElement, 38 const elem1 = key2 ? a[key1][key2] : a[key1]
39 shuffle, 39 const elem2 = key2 ? b[key1][key2] : b[key1]
40 arrayify 40
41 if (elem1 < elem2) return -1
42 if (elem1 === elem2) return 0
43 return 1
44 })
41} 45}
diff --git a/packages/core-utils/src/common/date.ts b/packages/core-utils/src/common/date.ts
index f0684ff86..66899de80 100644
--- a/packages/core-utils/src/common/date.ts
+++ b/packages/core-utils/src/common/date.ts
@@ -45,11 +45,13 @@ function isLastWeek (d: Date) {
45 45
46// --------------------------------------------------------------------------- 46// ---------------------------------------------------------------------------
47 47
48export const timecodeRegexString = `((\\d+)[h:])?((\\d+)[m:])?((\\d+)s?)?`
49
48function timeToInt (time: number | string) { 50function timeToInt (time: number | string) {
49 if (!time) return 0 51 if (!time) return 0
50 if (typeof time === 'number') return time 52 if (typeof time === 'number') return time
51 53
52 const reg = /^((\d+)[h:])?((\d+)[m:])?((\d+)s?)?$/ 54 const reg = new RegExp(`^${timecodeRegexString}$`)
53 const matches = time.match(reg) 55 const matches = time.match(reg)
54 56
55 if (!matches) return 0 57 if (!matches) return 0
diff --git a/packages/core-utils/src/index.ts b/packages/core-utils/src/index.ts
index 3ca5d9d47..69fa2c046 100644
--- a/packages/core-utils/src/index.ts
+++ b/packages/core-utils/src/index.ts
@@ -5,3 +5,4 @@ export * from './plugins/index.js'
5export * from './renderer/index.js' 5export * from './renderer/index.js'
6export * from './users/index.js' 6export * from './users/index.js'
7export * from './videos/index.js' 7export * from './videos/index.js'
8export * from './string/index.js'
diff --git a/packages/core-utils/src/string/chapters.ts b/packages/core-utils/src/string/chapters.ts
new file mode 100644
index 000000000..d7643665c
--- /dev/null
+++ b/packages/core-utils/src/string/chapters.ts
@@ -0,0 +1,32 @@
1import { timeToInt, timecodeRegexString } from '../common/date.js'
2
3const timecodeRegex = new RegExp(`^(${timecodeRegexString})\\s`)
4
5export function parseChapters (text: string) {
6 if (!text) return []
7
8 const lines = text.split(/\r?\n|\r|\n/g)
9 let foundChapters = false
10
11 const chapters: { timecode: number, title: string }[] = []
12
13 for (const line of lines) {
14 const matched = line.match(timecodeRegex)
15 if (!matched) {
16 // Stop chapters parsing
17 if (foundChapters) break
18
19 continue
20 }
21
22 foundChapters = true
23
24 const timecodeText = matched[1]
25 const timecode = timeToInt(timecodeText)
26 const title = line.replace(matched[0], '')
27
28 chapters.push({ timecode, title })
29 }
30
31 return chapters
32}
diff --git a/packages/core-utils/src/string/index.ts b/packages/core-utils/src/string/index.ts
new file mode 100644
index 000000000..42680ab16
--- /dev/null
+++ b/packages/core-utils/src/string/index.ts
@@ -0,0 +1 @@
export * from './chapters.js'