Skip to content

Commit

Permalink
Fixed issue with String.split not removing separator. Added `String…
Browse files Browse the repository at this point in the history
….join` class method.
  • Loading branch information
anthonyjb committed Aug 22, 2016
1 parent fcc7de0 commit 2410f11
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 18 deletions.
31 changes: 23 additions & 8 deletions build/html-string.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build/html-string.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "HTMLString",
"description": "An HTML parser written in JavaScript that's probably not what you're looking for.",
"version": "1.0.4",
"version": "1.0.5",
"keywords": [
"html",
"parser"
Expand Down
5 changes: 5 additions & 0 deletions spec/html-string-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,11 @@
describe('HTMLString.String.split()', function() {
return it('should split a string by the separator and return a list of sub-strings', function() {
var string, substrings;
string = new HTMLString.String(' ', true);
substrings = string.split(' ');
expect(substrings.length).toBe(2);
expect(substrings[0].length()).toBe(0);
expect(substrings[1].length()).toBe(0);
string = new HTMLString.String(quotes.Kernighan).trim();
substrings = string.split('a');
expect(substrings.length).toBe(11);
Expand Down
6 changes: 6 additions & 0 deletions src/spec/html-string-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,12 @@ describe 'HTMLString.String.split()', () ->

it 'should split a string by the separator and return a list of \
sub-strings', () ->
string = new HTMLString.String(' ', true)
substrings = string.split(' ')
expect(substrings.length).toBe 2
expect(substrings[0].length()).toBe 0
expect(substrings[1].length()).toBe 0

string = new HTMLString.String(quotes.Kernighan).trim()

substrings = string.split('a')
Expand Down
27 changes: 19 additions & 8 deletions src/strings.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ class HTMLString.String
if limit > 0 and count > limit
break
index = @indexOf(separator, lastIndex)
if index == -1 or index == (@length() - 1)
if index == -1
break
indexes.push(index)
lastIndex = index + 1
Expand All @@ -431,12 +431,16 @@ class HTMLString.String
# Build a list of sub-strings based on the split indexes
substrings = []
for i in [0..(indexes.length - 2)]
substrings.push(@slice(indexes[i], indexes[i + 1]))
start = indexes[i]
if i > 0
start += 1
end = indexes[i + 1]
substrings.push(@slice(start, end))

return substrings

startsWith: (substring) ->
# Return true if the sub=string starts with the specified string
# Return true if the string starts with the specified substring

# Compare to text
if typeof substring == 'string'
Expand Down Expand Up @@ -588,17 +592,24 @@ class HTMLString.String

# Class methods

@decode: (string) ->
# Decode entities within the specified string
textarea = document.createElement('textarea')
textarea.innerHTML = string
return textarea.textContent

@encode: (string) ->
# Encode entities within the specified string
textarea = document.createElement('textarea')
textarea.textContent = string
return textarea.innerHTML

@decode: (string) ->
# Decode entities within the specified string
textarea = document.createElement('textarea')
textarea.innerHTML = string
return textarea.textContent
@join: (separator, strings) ->
# Join a list of strings together
joined = strings.shift()
for s in strings
joined = joined.concat(separator, s)
return joined


# Constants
Expand Down

0 comments on commit 2410f11

Please sign in to comment.