Implemented the #{} key-value interpolate.
This commit is contained in:
parent
bb14481e83
commit
04423e1aaf
2 changed files with 120 additions and 47 deletions
110
spec/KiwiSpec.js
110
spec/KiwiSpec.js
|
|
@ -1,53 +1,75 @@
|
|||
describe('Kiwi.compose', function() {
|
||||
it('should return the empty string when input is empty',function() {
|
||||
var input = "";
|
||||
expect(Kiwi.compose(input)).toEqual(input);
|
||||
});
|
||||
describe('Kiwi.compose - %', function() {
|
||||
it('should return the empty string when input is empty',function() {
|
||||
var input = "";
|
||||
expect(Kiwi.compose(input)).toEqual(input);
|
||||
});
|
||||
|
||||
it('should return the input string when array is null',function() {
|
||||
var input = "text without interpolation";
|
||||
expect(Kiwi.compose(input)).toEqual(input);
|
||||
});
|
||||
it('should return the input string when array is null',function() {
|
||||
var input = "text without interpolation";
|
||||
expect(Kiwi.compose(input)).toEqual(input);
|
||||
});
|
||||
|
||||
it('should return replace %s with text', function() {
|
||||
var input = "There are % apples";
|
||||
var result = "There are five apples";
|
||||
expect(Kiwi.compose(input, ["five"])).toEqual(result);
|
||||
});
|
||||
it('should return replace %s with text', function() {
|
||||
var input = "There are % apples";
|
||||
var result = "There are five apples";
|
||||
expect(Kiwi.compose(input, ["five"])).toEqual(result);
|
||||
});
|
||||
|
||||
it('should return replace % with text when % is last', function() {
|
||||
var input = "There are five %";
|
||||
var result = "There are five apples";
|
||||
expect(Kiwi.compose(input, ["apples"])).toEqual(result);
|
||||
});
|
||||
it('should return replace % with text when % is last', function() {
|
||||
var input = "There are five %";
|
||||
var result = "There are five apples";
|
||||
expect(Kiwi.compose(input, ["apples"])).toEqual(result);
|
||||
});
|
||||
|
||||
it('should return replace % when input is %', function() {
|
||||
var input = "%";
|
||||
var result = "There are five apples";
|
||||
expect(Kiwi.compose(input, [result])).toEqual(result);
|
||||
});
|
||||
it('should return replace % when input is %', function() {
|
||||
var input = "%";
|
||||
var result = "There are five apples";
|
||||
expect(Kiwi.compose(input, [result])).toEqual(result);
|
||||
});
|
||||
|
||||
it('should return replace 2 %s when input has 2 %s', function() {
|
||||
var input = "There are % %";
|
||||
var result = "There are five apples";
|
||||
expect(Kiwi.compose(input, ["five", "apples"])).toEqual(result);
|
||||
});
|
||||
it('should return replace 2 %s when input has 2 %s', function() {
|
||||
var input = "There are % %";
|
||||
var result = "There are five apples";
|
||||
expect(Kiwi.compose(input, ["five", "apples"])).toEqual(result);
|
||||
});
|
||||
|
||||
it('should return replace 2 %s when input has 2 %s', function() {
|
||||
var input = "% are % apples";
|
||||
var result = "There are five apples";
|
||||
expect(Kiwi.compose(input, ["There", "five"])).toEqual(result);
|
||||
});
|
||||
it('should return replace 2 %s when input has 2 %s', function() {
|
||||
var input = "% are % apples";
|
||||
var result = "There are five apples";
|
||||
expect(Kiwi.compose(input, ["There", "five"])).toEqual(result);
|
||||
});
|
||||
|
||||
it('should return escaped %', function() {
|
||||
var input = "The % is 2.3`%";
|
||||
var result = "The rate is 2.3%";
|
||||
expect(Kiwi.compose(input, ["rate"])).toEqual(result);
|
||||
});
|
||||
it('should return escaped %', function() {
|
||||
var input = "The % is 2.3`%";
|
||||
var result = "The rate is 2.3%";
|
||||
expect(Kiwi.compose(input, ["rate"])).toEqual(result);
|
||||
});
|
||||
|
||||
it('should use an empty string when size of the array less fits the placeholders.', function() {
|
||||
var input = "The quick brown % jumps % the lazy %.";
|
||||
var result = "The quick brown fox jumps over the lazy .";
|
||||
expect(Kiwi.compose(input, ["fox", "over"])).toEqual(result);
|
||||
});
|
||||
it('should use an empty string when size of the array less fits the placeholders.', function() {
|
||||
var input = "The quick brown % jumps % the lazy %.";
|
||||
var result = "The quick brown fox jumps over the lazy .";
|
||||
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 TRANSPOSE_CHAR = '%';
|
||||
|
||||
var interpolate = function(text, array){
|
||||
if(array === undefined) return text;
|
||||
|
||||
var interpolate_general = function(text, array){
|
||||
var stringToReturn = ""
|
||||
,length = text.length
|
||||
,index = 0
|
||||
|
|
@ -31,6 +29,59 @@ var Kiwi = (function ($) {
|
|||
}
|
||||
}
|
||||
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 {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue