Created
December 6, 2016 16:17
-
-
Save TimFletcher/1fc43b36a2710f09895613ecdd7c39a7 to your computer and use it in GitHub Desktop.
Code to add a Regex type to Meteor's EJSON
This file contains hidden or 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
import { EJSON } from 'meteor/ejson'; | |
function getOptions(self) { | |
const opts = []; | |
if (self.global) opts.push('g'); | |
if (self.ignoreCase) opts.push('i'); | |
if (self.multiline) opts.push('m'); | |
return opts.join(''); | |
} | |
RegExp.prototype.clone = function clone() { | |
return new RegExp(this.source, getOptions(this)); | |
}; | |
RegExp.prototype.equals = function equals(other) { | |
if (!(other instanceof RegExp)) return false; | |
return EJSON.stringify(this) === EJSON.stringify(other); | |
}; | |
RegExp.prototype.typeName = function typeName() { | |
return 'RegExp'; | |
}; | |
RegExp.prototype.toJSONValue = function toJSONValue() { | |
return { regex: this.source, options: getOptions(this) }; | |
}; | |
EJSON.addType('RegExp', value => new RegExp(value.regex, value.options)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment