1 | 'use strict'
|
2 |
|
3 | const syllables = require('./syllables.json')
|
4 |
|
5 | const normalizePinyin = pinyin => {
|
6 | pinyin = pinyin.normalize('NFD').replace(/\u0304|\u0301|\u030c|\u0300/g, '')
|
7 | return pinyin.normalize('NFC').replace(/(\w|ü)[1-5]/gi, '$1').toLowerCase()
|
8 | }
|
9 |
|
10 | const split = (text, everything=false, wrapInList=false) => {
|
11 | const list = []
|
12 | let prevWordFound = false
|
13 | let wordEnd = text.length
|
14 | while (wordEnd > 0) {
|
15 | let count = wordEnd
|
16 | let wordFound = false
|
17 | while (count > 0) {
|
18 | const word = text.substring(wordEnd - count, wordEnd)
|
19 | if (syllables.includes(normalizePinyin(word))) {
|
20 | wordFound = true
|
21 | list.push(wrapInList ? [word] : word)
|
22 | wordEnd -= (count - 1)
|
23 | break
|
24 | }
|
25 | count--
|
26 | }
|
27 | if (!wordFound && everything) {
|
28 | const prevIndex = list.length - 1
|
29 | const prevEntry = list[prevIndex]
|
30 | if (wordEnd === text.length || typeof prevEntry === 'object' || prevWordFound) {
|
31 | list.push(text[wordEnd - 1])
|
32 | }
|
33 | else if (typeof prevEntry === 'string') {
|
34 | list[prevIndex] = text[wordEnd - 1] + prevEntry
|
35 | }
|
36 | }
|
37 | wordEnd --
|
38 | prevWordFound = wordFound
|
39 | }
|
40 | return list.reverse()
|
41 | }
|
42 |
|
43 | module.exports = split
|