Skip to content

Commit 2410f11

Browse files
committed
Fixed issue with String.split not removing separator. Added String.join class method.
1 parent fcc7de0 commit 2410f11

File tree

6 files changed

+55
-18
lines changed

6 files changed

+55
-18
lines changed

build/html-string.js

Lines changed: 23 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/html-string.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "HTMLString",
33
"description": "An HTML parser written in JavaScript that's probably not what you're looking for.",
4-
"version": "1.0.4",
4+
"version": "1.0.5",
55
"keywords": [
66
"html",
77
"parser"

spec/html-string-spec.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,11 @@
187187
describe('HTMLString.String.split()', function() {
188188
return it('should split a string by the separator and return a list of sub-strings', function() {
189189
var string, substrings;
190+
string = new HTMLString.String(' ', true);
191+
substrings = string.split(' ');
192+
expect(substrings.length).toBe(2);
193+
expect(substrings[0].length()).toBe(0);
194+
expect(substrings[1].length()).toBe(0);
190195
string = new HTMLString.String(quotes.Kernighan).trim();
191196
substrings = string.split('a');
192197
expect(substrings.length).toBe(11);

src/spec/html-string-spec.coffee

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,12 @@ describe 'HTMLString.String.split()', () ->
229229

230230
it 'should split a string by the separator and return a list of \
231231
sub-strings', () ->
232+
string = new HTMLString.String(' ', true)
233+
substrings = string.split(' ')
234+
expect(substrings.length).toBe 2
235+
expect(substrings[0].length()).toBe 0
236+
expect(substrings[1].length()).toBe 0
237+
232238
string = new HTMLString.String(quotes.Kernighan).trim()
233239

234240
substrings = string.split('a')

src/strings.coffee

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ class HTMLString.String
421421
if limit > 0 and count > limit
422422
break
423423
index = @indexOf(separator, lastIndex)
424-
if index == -1 or index == (@length() - 1)
424+
if index == -1
425425
break
426426
indexes.push(index)
427427
lastIndex = index + 1
@@ -431,12 +431,16 @@ class HTMLString.String
431431
# Build a list of sub-strings based on the split indexes
432432
substrings = []
433433
for i in [0..(indexes.length - 2)]
434-
substrings.push(@slice(indexes[i], indexes[i + 1]))
434+
start = indexes[i]
435+
if i > 0
436+
start += 1
437+
end = indexes[i + 1]
438+
substrings.push(@slice(start, end))
435439

436440
return substrings
437441

438442
startsWith: (substring) ->
439-
# Return true if the sub=string starts with the specified string
443+
# Return true if the string starts with the specified substring
440444

441445
# Compare to text
442446
if typeof substring == 'string'
@@ -588,17 +592,24 @@ class HTMLString.String
588592

589593
# Class methods
590594

595+
@decode: (string) ->
596+
# Decode entities within the specified string
597+
textarea = document.createElement('textarea')
598+
textarea.innerHTML = string
599+
return textarea.textContent
600+
591601
@encode: (string) ->
592602
# Encode entities within the specified string
593603
textarea = document.createElement('textarea')
594604
textarea.textContent = string
595605
return textarea.innerHTML
596606

597-
@decode: (string) ->
598-
# Decode entities within the specified string
599-
textarea = document.createElement('textarea')
600-
textarea.innerHTML = string
601-
return textarea.textContent
607+
@join: (separator, strings) ->
608+
# Join a list of strings together
609+
joined = strings.shift()
610+
for s in strings
611+
joined = joined.concat(separator, s)
612+
return joined
602613

603614

604615
# Constants

0 commit comments

Comments
 (0)