Skip to content

Commit

Permalink
refactor: remove strSplit function
Browse files Browse the repository at this point in the history
  • Loading branch information
oakmac committed Nov 30, 2024
1 parent 49ee735 commit 5544e13
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 29 deletions.
27 changes: 12 additions & 15 deletions lib/standard-clojure-style.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,6 @@
return txt.replace(/\r\n/g, '\n')
}

function strSplit (str, ch) {
return str.split(ch)
}

// ---------------------------------------------------------------------------
// id generator

Expand Down Expand Up @@ -1142,12 +1138,9 @@

function isNewlineNodeWithCommaOnNextLine (n) {
if (n && isNewlineNode(n)) {
const txtSlices = strSplit(n.text, '\n')
if (txtSlices.length >= 2) {
const lastSlice = arrayLast(txtSlices)
if (strIncludes(lastSlice, ',')) {
return true
}
const tailStr = removeCharsUpToNewline(n.text)
if (strIncludes(tailStr, ',')) {
return true
}
}

Expand Down Expand Up @@ -1188,9 +1181,7 @@
}

function numSpacesAfterNewline (newlineNode) {
const x = strSplit(newlineNode.text, '\n')
const lastX = arrayLast(x)
return strLen(lastX)
return strLen(removeCharsUpToNewline(newlineNode.text))
}

// adds _origColIdx to the nodes on this line, stopping when we reach the next newline node
Expand Down Expand Up @@ -1239,6 +1230,11 @@
return txt.replace(/[, ]*$/, '')
}

function removeCharsUpToNewline (txt) {
const slices = txt.split('\n')
return slices[slices.length - 1]
}

function txtHasCommasAfterNewline (s) {
return /\n.*,.*$/.test(s)
}
Expand Down Expand Up @@ -1320,7 +1316,7 @@
}

function parseJavaPackageWithClass (s) {
const chunks = strSplit(s, '.')
const chunks = s.split('.')
const lastItm = arrayLast(chunks)

if (looksLikeAJavaClassname(lastItm)) {
Expand Down Expand Up @@ -3954,8 +3950,8 @@
API._strEndsWith = strEndsWith
API._isStringWithChars = isStringWithChars
API._strReplaceFirst = strReplaceFirst
API._strReplaceAll = strReplaceAll
API._crlfToLf = crlfToLf
API._strSplit = strSplit
API._stackPeek = stackPeek
API._stackPop = stackPop
API._stackPush = stackPush
Expand All @@ -3964,6 +3960,7 @@
API._commentNeedsSpaceInside = commentNeedsSpaceInside
API._removeLeadingWhitespace = removeLeadingWhitespace
API._removeTrailingWhitespace = removeTrailingWhitespace
API._removeCharsUpToNewline = removeCharsUpToNewline
API._txtHasCommasAfterNewline = txtHasCommasAfterNewline

API._AnyChar = AnyChar
Expand Down
27 changes: 13 additions & 14 deletions test/internals.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,12 @@ describe('String Util', () => {
})
})

describe('strReplaceAll', () => {
expect(scsLib._strReplaceAll('aaa', 'a', 'b')).toBe('bbb')
expect(scsLib._strReplaceAll('a,b,c,', ',', '')).toBe('abc')
expect(scsLib._strReplaceAll('a,b,c,', ',', 'x')).toBe('axbxcx')
})

describe('crlfToLf', () => {
test('converts CRLF to LF', () => {
expect(scsLib._crlfToLf('hello\r\nworld')).toBe('hello\nworld')
Expand All @@ -183,20 +189,6 @@ describe('String Util', () => {
expect(scsLib._crlfToLf('\r\n')).toBe('\n')
})
})

describe('strSplit', () => {
test('splits string by delimiter', () => {
expect(scsLib._strSplit('a-b-c', '-')).toEqual(['a', 'b', 'c'])
expect(scsLib._strSplit('hello world', ' ')).toEqual(['hello', 'world'])
})

test('handles edge cases', () => {
expect(scsLib._strSplit('', '-')).toEqual([''])
expect(scsLib._strSplit('hello', '')).toEqual(['h', 'e', 'l', 'l', 'o'])
expect(scsLib._strSplit('a', 'x')).toEqual(['a'])
expect(scsLib._strSplit('a-b-', '-')).toEqual(['a', 'b', ''])
})
})
})

describe('Stack Operations Tests', () => {
Expand Down Expand Up @@ -359,6 +351,13 @@ test('removeTrailingWhitespace', () => {
expect(scsLib._removeTrailingWhitespace('aaa \n ')).toBe('aaa \n')
})

test('removeCharsUpToNewline', () => {
expect(isFn(scsLib._removeCharsUpToNewline)).toBe(true)
expect(scsLib._removeCharsUpToNewline('abc\nxyz')).toBe('xyz')
expect(scsLib._removeCharsUpToNewline('abc')).toBe('abc')
expect(scsLib._removeCharsUpToNewline('abc\ndef\n\nxyz')).toBe('xyz')
})

test('txtHasCommasAfterNewline', () => {
expect(scsLib._txtHasCommasAfterNewline('\n ,,')).toBe(true)
expect(scsLib._txtHasCommasAfterNewline('\n\n ,')).toBe(true)
Expand Down

0 comments on commit 5544e13

Please sign in to comment.