kopia lustrzana https://github.com/c9/core
Merge pull request +10145 from c9/fix-regexp-in-settings
Fix regexp in settingspull/223/head
commit
65a0dc39c8
|
@ -0,0 +1,28 @@
|
|||
/**
|
||||
* JSON (de-)serializer with support for encosing regular expressions
|
||||
*/
|
||||
"use strict";
|
||||
|
||||
exports.replacer = function(key, value) {
|
||||
if (value instanceof RegExp)
|
||||
return ("__REGEXP " + value.toString());
|
||||
else
|
||||
return value;
|
||||
};
|
||||
|
||||
exports.reviver = function(key, value) {
|
||||
if ((value + "").indexOf("__REGEXP ") == 0) {
|
||||
var m = value.match(/__REGEXP \/(.*)\/(.*)?/);
|
||||
return new RegExp(m[1], m[2]);
|
||||
}
|
||||
else
|
||||
return value;
|
||||
};
|
||||
|
||||
exports.stringify = function(value, space) {
|
||||
return JSON.stringify(value, exports.replacer, space);
|
||||
};
|
||||
|
||||
exports.parse = function(rext) {
|
||||
return JSON.parse(rext, exports.reviver);
|
||||
};
|
|
@ -0,0 +1,27 @@
|
|||
/*global describe it before after beforeEach afterEach define*/
|
||||
"use strict";
|
||||
"use server";
|
||||
"use mocha";
|
||||
|
||||
require("c9/inline-mocha")(module);
|
||||
|
||||
var assert = require("assert-diff");
|
||||
var reJSON = require("./json-with-re");
|
||||
|
||||
describe(__filename, function(){
|
||||
it("should encode regular expressions", function() {
|
||||
assert.deepEqual(reJSON.stringify({ foo: /foo/ }), '{"foo":"__REGEXP /foo/"}');
|
||||
assert.deepEqual(reJSON.stringify({ foo: /foo\//gi }), "{\"foo\":\"__REGEXP /foo\\\\//gi\"}");
|
||||
});
|
||||
it("should decode regular expressions", function() {
|
||||
assert.deepEqual(reJSON.parse('{"foo":"__REGEXP /foo/"}'), { foo: /foo/ });
|
||||
assert.deepEqual(reJSON.parse("{\"foo\":\"__REGEXP /foo\\\\//gi\"}"), { foo: /foo\//gi });
|
||||
});
|
||||
it("should deal with null values", function() {
|
||||
var o = {
|
||||
foo: null,
|
||||
bar: /dd/
|
||||
};
|
||||
assert.deepEqual(reJSON.parse(reJSON.stringify(o)), o);
|
||||
});
|
||||
});
|
Ładowanie…
Reference in New Issue