forked from Pirate-Weather/translations
-
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.
- Loading branch information
J. T. L
committed
Sep 6, 2016
1 parent
fafa396
commit cf97e3f
Showing
6 changed files
with
209 additions
and
240 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
var fs = require("fs"), | ||
path = require("path"); | ||
"use strict"; | ||
const fs = require("fs"), | ||
path = require("path"); | ||
|
||
fs.readdirSync(path.join(__dirname, "lib/lang")).forEach(function(pathname) { | ||
var match = /^([^\.].*)\.js$/.exec(pathname); | ||
fs.readdirSync(path.join(__dirname, "lib/lang")).forEach(pathname => { | ||
const match = /^([^\.].*)\.js$/.exec(pathname); | ||
|
||
if(match) | ||
if(match) { | ||
exports[match[1]] = require("./lib/lang/" + pathname); | ||
} | ||
}); |
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
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 |
---|---|---|
@@ -1,80 +1,98 @@ | ||
module.exports = function(template) { | ||
function parse(expr, stack) { | ||
var result; | ||
"use strict"; | ||
|
||
if(typeof expr === "number") { | ||
stack.push(expr); | ||
function parse(template, expr, stack) { | ||
if(typeof expr === "number") { | ||
return expr.toString(); | ||
} | ||
|
||
let result; | ||
|
||
result = expr.toString(); | ||
if(typeof expr === "string") { | ||
if(!template.hasOwnProperty(expr)) { | ||
throw new Error("\"" + expr + "\" not found in language template."); | ||
} | ||
|
||
else if(typeof expr === "string") { | ||
stack.push(expr); | ||
stack.push(expr); | ||
|
||
if(!template.hasOwnProperty(expr)) | ||
throw new Error("\"" + expr + "\" not found in language template."); | ||
if(typeof template[expr] === "string") { | ||
if(/\$\d+/.test(template[expr])) { | ||
throw new Error( | ||
"\"" + expr + "\" was used in a value context, " + | ||
"but is expected in a template context." | ||
); | ||
} | ||
|
||
else if(typeof template[expr] === "string") { | ||
if(/\$\d+/.test(template[expr])) | ||
throw new Error("\"" + expr + "\" was used in a value context, but is expected in a template context."); | ||
result = template[expr]; | ||
} | ||
|
||
else | ||
result = template[expr]; | ||
else if(typeof template[expr] === "function") { | ||
if(template[expr].length !== 0) { | ||
throw new Error( | ||
"\"" + expr + "\" was used in a value context, " + | ||
"but is expected in a template context." | ||
); | ||
} | ||
|
||
else if(typeof template[expr] === "function") { | ||
if(template[expr].length !== 0) | ||
throw new Error("\"" + expr + "\" was used in a value context, but is expected in a template context."); | ||
result = template[expr].call(stack); | ||
} | ||
|
||
else | ||
result = template[expr].call(stack); | ||
} | ||
else { | ||
throw new Error( | ||
"\"" + expr + "\" is not a valid language template pattern." | ||
); | ||
} | ||
} | ||
|
||
else | ||
throw new Error("\"" + expr + "\" is not a valid language template pattern."); | ||
else if(Array.isArray(expr) && | ||
expr.length && | ||
typeof expr[0] === "string") { | ||
if(!template.hasOwnProperty(expr[0])) { | ||
throw new Error("\"" + expr[0] + "\" not found in language template."); | ||
} | ||
|
||
else if(Array.isArray(expr) && | ||
expr.length && | ||
typeof expr[0] === "string") { | ||
stack.push(expr[0]); | ||
|
||
if(!template.hasOwnProperty(expr[0])) | ||
throw new Error("\"" + expr[0] + "\" not found in language template."); | ||
|
||
else if(typeof template[expr[0]] === "string") | ||
result = template[expr[0]].replace(/\$\d+/g, function(n) { | ||
return parse(expr[n.slice(1)|0], stack); | ||
}); | ||
|
||
else if(typeof template[expr[0]] === "function") { | ||
if(template[expr[0]].length === 0) | ||
throw new Error("\"" + expr[0] + "\" was used in a template context, but is expected in a value context."); | ||
|
||
else if(template[expr[0]].length !== expr.length - 1) | ||
throw new Error("Template \"" + expr[0] + "\" did not expect " + (expr.length - 1) + " arguments."); | ||
|
||
else | ||
result = template[expr[0]].apply( | ||
stack, | ||
expr.slice(1).map(function(arg) { | ||
return parse(arg, stack); | ||
}) | ||
); | ||
} | ||
stack.push(expr[0]); | ||
|
||
else | ||
throw new Error("\"" + expr[0] + "\" is not a valid language template pattern."); | ||
if(typeof template[expr[0]] === "string") { | ||
result = template[expr[0]].replace( | ||
/\$\d+/g, | ||
n => parse(template, expr[n.slice(1)|0], stack) | ||
); | ||
} | ||
|
||
else | ||
throw new Error("Invalid expression."); | ||
else if(typeof template[expr[0]] === "function") { | ||
if(template[expr[0]].length === 0) { | ||
throw new Error( | ||
"\"" + expr[0] + "\" was used in a template context, " + | ||
"but is expected in a value context." | ||
); | ||
} | ||
|
||
stack.pop(); | ||
return result; | ||
}; | ||
if(template[expr[0]].length !== expr.length - 1) { | ||
throw new Error( | ||
"Template \"" + expr[0] + "\" did not expect " + | ||
(expr.length - 1) + " arguments." | ||
); | ||
} | ||
|
||
return function(expr) { | ||
return parse(expr, []); | ||
result = template[expr[0]].apply( | ||
stack, | ||
expr.slice(1).map(arg => parse(template, arg, stack)) | ||
); | ||
} | ||
|
||
else { | ||
throw new Error( | ||
"\"" + expr[0] + "\" is not a valid language template pattern." | ||
); | ||
} | ||
} | ||
}; | ||
|
||
else { | ||
throw new Error("Invalid expression."); | ||
} | ||
|
||
stack.pop(); | ||
return result; | ||
} | ||
|
||
module.exports = template => expr => parse(template, expr, []); |
Oops, something went wrong.