From 5544e1300dac9503ec06852ec071114e53825077 Mon Sep 17 00:00:00 2001 From: Chris Oakman Date: Fri, 29 Nov 2024 22:22:21 -0600 Subject: [PATCH] refactor: remove strSplit function --- lib/standard-clojure-style.js | 27 ++++++++++++--------------- test/internals.test.js | 27 +++++++++++++-------------- 2 files changed, 25 insertions(+), 29 deletions(-) diff --git a/lib/standard-clojure-style.js b/lib/standard-clojure-style.js index 300d42c..144ba1a 100644 --- a/lib/standard-clojure-style.js +++ b/lib/standard-clojure-style.js @@ -251,10 +251,6 @@ return txt.replace(/\r\n/g, '\n') } - function strSplit (str, ch) { - return str.split(ch) - } - // --------------------------------------------------------------------------- // id generator @@ -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 } } @@ -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 @@ -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) } @@ -1320,7 +1316,7 @@ } function parseJavaPackageWithClass (s) { - const chunks = strSplit(s, '.') + const chunks = s.split('.') const lastItm = arrayLast(chunks) if (looksLikeAJavaClassname(lastItm)) { @@ -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 @@ -3964,6 +3960,7 @@ API._commentNeedsSpaceInside = commentNeedsSpaceInside API._removeLeadingWhitespace = removeLeadingWhitespace API._removeTrailingWhitespace = removeTrailingWhitespace + API._removeCharsUpToNewline = removeCharsUpToNewline API._txtHasCommasAfterNewline = txtHasCommasAfterNewline API._AnyChar = AnyChar diff --git a/test/internals.test.js b/test/internals.test.js index 2dac442..df29dfd 100644 --- a/test/internals.test.js +++ b/test/internals.test.js @@ -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') @@ -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', () => { @@ -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)