Skip to content

Commit

Permalink
Add RegExp serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
bluepnume committed Sep 5, 2017
1 parent 648686d commit 07aebc8
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ post-robot will serialize and deserialize the following data types in messages:
- Promises
- Note: deserialized promises will be instances of [`ZalgoPromise`](https://github.com/krakenjs/zalgo-promise)
- Error objects
- Regex objects

## Simple listener and sender

Expand Down
3 changes: 2 additions & 1 deletion src/conf/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ export let CONSTANTS = {
METHOD: 'postrobot_method',
ERROR: 'postrobot_error',
PROMISE: 'postrobot_promise',
ZALGO_PROMISE: 'postrobot_zalgo_promise'
ZALGO_PROMISE: 'postrobot_zalgo_promise',
REGEX: 'regex'
},

SEND_STRATEGIES: {
Expand Down
27 changes: 26 additions & 1 deletion src/lib/serialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { matchDomain } from 'cross-domain-utils/src';
import { ZalgoPromise } from 'zalgo-promise/src';

import { CONSTANTS } from '../conf';
import { once, uniqueID, replaceObject, stringifyError } from './util';
import { once, uniqueID, replaceObject, stringifyError, isRegex } from './util';
import { on, send } from '../interface';
import { log } from './log';
import { global } from '../global';
Expand Down Expand Up @@ -108,6 +108,18 @@ function serializeZalgoPromise(destination : any, domain : string, promise : Zal
};
}

type SerializedRegex = {
__type__ : string,
__source__ : string
};

function serializeRegex(regex : RegExp) : SerializedRegex {
return {
__type__: CONSTANTS.SERIALIZATION_TYPES.REGEX,
__source__: regex.source
};
}

export function serializeMethods(destination : any, domain : string, obj : Object) : Object {

return replaceObject({ obj }, (item, key) => {
Expand All @@ -127,6 +139,11 @@ export function serializeMethods(destination : any, domain : string, obj : Objec
// $FlowFixMe
return serializeZalgoPromise(destination, domain, item, key.toString());
}

if (isRegex(item)) {
// $FlowFixMe
return serializeRegex(item);
}
}).obj;
}

Expand Down Expand Up @@ -175,6 +192,10 @@ export function deserializePromise(source : any, origin : string, prom : Object)
return new window.Promise((resolve, reject) => deserializeMethod(source, origin, prom.__then__)(resolve, reject));
}

export function deserializeRegex(source : any, origin : string, item : Object) : RegExp {
return new RegExp(item.__source__);
}

export function deserializeMethods(source : any, origin : string, obj : Object) : Object {

return replaceObject({ obj }, (item, key) => {
Expand All @@ -198,5 +219,9 @@ export function deserializeMethods(source : any, origin : string, obj : Object)
return deserializeZalgoPromise(source, origin, item);
}

if (isSerialized(item, CONSTANTS.SERIALIZATION_TYPES.REGEX)) {
return deserializeRegex(source, origin, item);
}

}).obj;
}

0 comments on commit 07aebc8

Please # to comment.