Skip to content

Commit

Permalink
small refactor to substr function to ease with porting
Browse files Browse the repository at this point in the history
  • Loading branch information
oakmac committed Nov 1, 2024
1 parent e7d4905 commit 3c29320
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
17 changes: 8 additions & 9 deletions lib/standard-clojure-style.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,13 @@
return s.charAt(n)
}

// Returns the substring of s beginning at start inclusive, and ending
// at end (defaults to length of string), exclusive.
function substr (s, start, end) {
// Returns the substring of s beginning at startIdx inclusive, and ending
// at endIdx, exclusive.
// Pass -1 to endIdx to mean "until the end of the string"
function substr (s, startIdx, endIdx) {
const len = strLen(s)
if (!isPositiveInt(end)) end = len
if (end > len) end = len
// TODO: throw here if end < start?
return s.substring(start, end)
if (endIdx < 0) endIdx = len
return s.substring(startIdx, endIdx)
}

function repeatString (text, n) {
Expand Down Expand Up @@ -399,7 +398,7 @@
pattern_str: opts.regex,
parse: (txt, pos) => {
// NOTE: this might be a perf issue; investigate later
const txt2 = substr(txt, pos)
const txt2 = substr(txt, pos, -1)
const result = txt2.match(opts.regex)

// HACK HACK HACK:
Expand Down Expand Up @@ -2335,7 +2334,7 @@
} else if (insideGenClass && idx > genClassNodeIdx && isTextNode && genClassToggle === 0 && isGenClassKeyword(node)) {
result.genClass.isEmpty = false

genClassKeyStr = substr(node.text, 1)
genClassKeyStr = substr(node.text, 1, -1)
result.genClass[genClassKeyStr] = {}

// add commentsAbove if possible
Expand Down
2 changes: 1 addition & 1 deletion test/internals.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ test('String util: substr', () => {
expect(scsLib._substr('abcdef', 0, 0)).toBe('')
expect(scsLib._substr('abcdef', 0, 2)).toBe('ab')
expect(scsLib._substr('abcdef', 3, 5)).toBe('de')
expect(scsLib._substr('abcdef', 2)).toBe('cdef')
expect(scsLib._substr('abcdef', 2, -1)).toBe('cdef')
})

test('commentNeedsSpaceBefore', () => {
Expand Down

0 comments on commit 3c29320

Please sign in to comment.