Skip to content

Instantly share code, notes, and snippets.

@TimFletcher
Created December 6, 2016 16:17
Show Gist options
  • Save TimFletcher/1fc43b36a2710f09895613ecdd7c39a7 to your computer and use it in GitHub Desktop.
Save TimFletcher/1fc43b36a2710f09895613ecdd7c39a7 to your computer and use it in GitHub Desktop.
Code to add a Regex type to Meteor's EJSON
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