Implemented the #{} key-value interpolate.
This commit is contained in:
parent
bb14481e83
commit
04423e1aaf
2 changed files with 120 additions and 47 deletions
|
|
@ -1,4 +1,4 @@
|
||||||
describe('Kiwi.compose', function() {
|
describe('Kiwi.compose - %', function() {
|
||||||
it('should return the empty string when input is empty',function() {
|
it('should return the empty string when input is empty',function() {
|
||||||
var input = "";
|
var input = "";
|
||||||
expect(Kiwi.compose(input)).toEqual(input);
|
expect(Kiwi.compose(input)).toEqual(input);
|
||||||
|
|
@ -51,3 +51,25 @@ describe('Kiwi.compose', function() {
|
||||||
expect(Kiwi.compose(input, ["fox", "over"])).toEqual(result);
|
expect(Kiwi.compose(input, ["fox", "over"])).toEqual(result);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('Kiwi.compose - %{}', function() {
|
||||||
|
it('should replace the variable defined in %{}',function() {
|
||||||
|
var input = "The quick brown %{f} jumps over the lazy %{d}.";
|
||||||
|
var dic = {"f": "fox", "d": "dog"};
|
||||||
|
var output = "The quick brown fox jumps over the lazy dog.";
|
||||||
|
expect(Kiwi.compose(input, dic)).toEqual(output);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should throw a syntax error.',function() {
|
||||||
|
var input = "The quick brown %{f jumps over the lazy dog.";
|
||||||
|
var dic = {"f": "fox", "d": "dog"};
|
||||||
|
expect(function(){Kiwi.compose(input, dic)}).toThrow(Error("Syntax error."));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should ignore when the variable is not available',function() {
|
||||||
|
var input = "The quick brown %{f} jumps over the lazy dog.";
|
||||||
|
var dic = {"d": "dog"};
|
||||||
|
var output = "The quick brown jumps over the lazy dog.";
|
||||||
|
expect(Kiwi.compose(input, dic)).toEqual(output);
|
||||||
|
});
|
||||||
|
});
|
||||||
57
src/Kiwi.js
57
src/Kiwi.js
|
|
@ -1,9 +1,7 @@
|
||||||
var Kiwi = (function ($) {
|
var Kiwi = (function ($) {
|
||||||
var TRANSPOSE_CHAR = '%';
|
var TRANSPOSE_CHAR = '%';
|
||||||
|
|
||||||
var interpolate = function(text, array){
|
var interpolate_general = function(text, array){
|
||||||
if(array === undefined) return text;
|
|
||||||
|
|
||||||
var stringToReturn = ""
|
var stringToReturn = ""
|
||||||
,length = text.length
|
,length = text.length
|
||||||
,index = 0
|
,index = 0
|
||||||
|
|
@ -31,6 +29,59 @@ var Kiwi = (function ($) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return stringToReturn;
|
return stringToReturn;
|
||||||
|
}
|
||||||
|
|
||||||
|
var get_key_length = function(text, index){
|
||||||
|
var length = text.length;
|
||||||
|
|
||||||
|
var key = "";
|
||||||
|
|
||||||
|
var i = index;
|
||||||
|
while(i < length && text[i] !== '}'){
|
||||||
|
key += text[i];
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(text[i] === '}') key += '}';
|
||||||
|
else throw Error("Syntax error.");
|
||||||
|
|
||||||
|
return [key.slice(2, key.length - 1), key.length];
|
||||||
|
}
|
||||||
|
|
||||||
|
var interpolate_key_value = function(text, json){
|
||||||
|
var stringToReturn = ""
|
||||||
|
,length = text.length
|
||||||
|
,index = 0;
|
||||||
|
|
||||||
|
for(var i = 0; i < length; i++){
|
||||||
|
var currentChar = text[i];
|
||||||
|
var nextChar = null;
|
||||||
|
if(i + 1 < length) nextChar = text[i+1];
|
||||||
|
|
||||||
|
if(currentChar === '%' && nextChar === '{'){
|
||||||
|
var result = get_key_length(text, i);
|
||||||
|
var key = result[0];
|
||||||
|
var key_length = result[1];
|
||||||
|
|
||||||
|
stringToReturn += (json[key] === undefined ? "" : json[key]);
|
||||||
|
i += key_length - 1 ;
|
||||||
|
|
||||||
|
}else{
|
||||||
|
stringToReturn += currentChar;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return stringToReturn;
|
||||||
|
}
|
||||||
|
|
||||||
|
var interpolate = function(text, input){
|
||||||
|
if(input === undefined) return text;
|
||||||
|
|
||||||
|
if(Object.prototype.toString.call(input) === "[object Array]"){
|
||||||
|
return interpolate_general(text, input);
|
||||||
|
}else{
|
||||||
|
return interpolate_key_value(text, input);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue