forked from discourse/discourse
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract breaking up a string into a separate file, include it in
embedded view.
- Loading branch information
Showing
4 changed files
with
73 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,3 +38,4 @@ | |
//= require show-html.js | ||
//= require lock-on.js | ||
//= require ember-cloaking | ||
//= require break_string |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
(function() { | ||
|
||
var BREAK = "<wbr>​"; | ||
|
||
/** | ||
A class for intelligently breaking up strings at logical points. | ||
**/ | ||
var BreakString = function(string) { | ||
this.string = string; | ||
}; | ||
|
||
BreakString.prototype.break = function(hint) { | ||
var hintPos = [], | ||
str = this.string; | ||
|
||
if(hint) { | ||
hint = hint.toLowerCase().split(/\s+/).reverse(); | ||
var current = 0; | ||
while(hint.length > 0) { | ||
var word = hint.pop(); | ||
if(word !== str.substr(current, word.length).toLowerCase()) { | ||
break; | ||
} | ||
current += word.length; | ||
hintPos.push(current); | ||
} | ||
} | ||
|
||
var rval = [], | ||
prev = str[0]; | ||
rval.push(prev); | ||
for (var i=1;i<str.length;i++) { | ||
var cur = str[i]; | ||
if(prev.match(/[^0-9]/) && cur.match(/[0-9]/)){ | ||
rval.push(BREAK); | ||
} else if(i>1 && prev.match(/[A-Z]/) && cur.match(/[a-z]/)){ | ||
rval.pop(); | ||
rval.push(BREAK); | ||
rval.push(prev); | ||
} else if(prev.match(/[^A-Za-z0-9]/) && cur.match(/[a-zA-Z0-9]/)){ | ||
rval.push(BREAK); | ||
} else if(hintPos.indexOf(i) > -1) { | ||
rval.push(BREAK); | ||
} | ||
|
||
rval.push(cur); | ||
prev = cur; | ||
} | ||
return rval.join(""); | ||
}; | ||
|
||
this.BreakString = BreakString; | ||
|
||
}).call(this); |