-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ability to compare the library's results with .NET (`node src\stringf…
…ormat.tests.js dotnet`)
- Loading branch information
Showing
5 changed files
with
150 additions
and
6 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 |
---|---|---|
|
@@ -2,3 +2,4 @@ bower_components/ | |
node_modules/ | ||
tmp/ | ||
/build | ||
*.exe |
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,57 @@ | ||
#pragma warning disable 0649 | ||
|
||
using System; | ||
using System.Globalization; | ||
using System.Text; | ||
using System.Web.Script.Serialization; | ||
|
||
class Arg | ||
{ | ||
public string Format, Culture; | ||
public object[] Args = new object[0]; | ||
} | ||
|
||
class Result | ||
{ | ||
public string ReturnValue, Format, Error; | ||
} | ||
|
||
public class Checker | ||
{ | ||
static JavaScriptSerializer serializer = new JavaScriptSerializer(); | ||
|
||
static Result Process(string[] args) { | ||
if (args == null || args.Length != 1) { | ||
return new Result { Error = "Invalid argument" }; | ||
} | ||
Arg arg; | ||
try { | ||
arg = serializer.Deserialize<Arg>(args[0]); | ||
} catch { | ||
return new Result { Error = "Invalid JSON: " + args[0] }; | ||
} | ||
try { | ||
var culture = arg.Culture == null ? CultureInfo.InvariantCulture : CultureInfo.GetCultureInfo(arg.Culture); | ||
//System.Threading.Thread.CurrentThread.CurrentCulture = culture; | ||
return new Result { ReturnValue = String.Format(culture, arg.Format, arg.Args), Format = arg.Format }; | ||
} catch (Exception e) { | ||
return new Result { Error = e.Message }; | ||
} | ||
} | ||
|
||
public static void Main(string[] args) { | ||
Console.WriteLine(UnicodeEscape(serializer.Serialize(Process(args)))); | ||
} | ||
|
||
static string UnicodeEscape(string s) { | ||
var sb = new StringBuilder(); | ||
foreach (var c in s) { | ||
if (c > 127) { | ||
sb.Append(String.Format(@"\u{0:X4}", Convert.ToInt32(c))); | ||
} else { | ||
sb.Append(c); | ||
} | ||
} | ||
return sb.ToString(); | ||
} | ||
} |
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,63 @@ | ||
/*jshint undef:true, node:true*/ | ||
/*global sffjs*/ | ||
"use strict"; | ||
|
||
var child_process = require('child_process'), | ||
path = require('path'), | ||
fs = require('fs'); | ||
|
||
var exe = path.join(__dirname, 'checker.exe'), | ||
src = path.join(__dirname, 'checker.cs'); | ||
|
||
if (!fs.existsSync(exe)) { | ||
child_process.execSync('csc /out:' + JSON.stringify(exe) + ' ' + JSON.stringify(src), { encoding: 'utf8' }); | ||
} | ||
if (!fs.existsSync(exe)) { | ||
throw new Error('Can\'t find checker.exe'); | ||
} | ||
|
||
module.exports = function() { | ||
var arg = { | ||
format: arguments[0], | ||
args: Array.prototype.slice.call(arguments, 1), | ||
culture: sffjs.LC.name || undefined | ||
}; | ||
var resultJson = child_process.execFileSync(exe, [stringifyJsonCommandLineArgument(arg)], { encoding: 'utf8' }); | ||
var result = JSON.parse(resultJson); | ||
if (result.Error) { | ||
throw new Error(result.Error); | ||
} else if (arg.format !== result.Format) { | ||
throw new Error('Format string got corrupted "' + arg.format + '" -> "' + result.Format + '"'); | ||
} else { | ||
return result.ReturnValue; | ||
} | ||
}; | ||
|
||
function padWithLeadingZeros(string) { | ||
return new Array(5 - string.length).join("0") + string; | ||
} | ||
|
||
function unicodeCharEscape(charCode) { | ||
return "\\u" + padWithLeadingZeros(charCode.toString(16)); | ||
} | ||
|
||
function unicodeEscape(string) { | ||
return string.split("") | ||
.map(function(char) { | ||
var charCode = char.charCodeAt(0); | ||
return charCode > 127 ? unicodeCharEscape(charCode) : char; | ||
}) | ||
.join(""); | ||
} | ||
|
||
function stringifyJsonCommandLineArgument(arg) { | ||
var originalPrototypeDateToJSON = Date.prototype.toJSON; | ||
Date.prototype.toJSON = function() { | ||
return '/Date(' + (this.valueOf() - this.getTimezoneOffset() * 60000) + ')/'; // FIXME | ||
}; | ||
var argJson = unicodeEscape(JSON.stringify(arg)) | ||
.replace(/(\/Date\(\d+\))\//g, '\\$1\\/') | ||
.replace(/\\\\/g, '\\u005c'); | ||
Date.prototype.toJSON = originalPrototypeDateToJSON; | ||
return argJson; | ||
} |
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