2019-11-13 14:01:51 +00:00
|
|
|
import { Type as YamlType } from 'js-yaml';
|
|
|
|
import { isNil } from 'lodash';
|
|
|
|
|
2019-11-13 14:20:34 +00:00
|
|
|
import { InvalidArgumentError } from '../error/InvalidArgumentError';
|
2019-11-13 14:01:51 +00:00
|
|
|
|
|
|
|
export const REGEXP_REGEXP = /^\/(.+)\/([gimsuy]*)$/;
|
|
|
|
|
|
|
|
export const regexpType = new YamlType('!regexp', {
|
|
|
|
kind: 'scalar',
|
|
|
|
resolve(value: string) {
|
|
|
|
return REGEXP_REGEXP.test(value);
|
|
|
|
},
|
|
|
|
construct(value: string): RegExp {
|
|
|
|
const match = REGEXP_REGEXP.exec(value);
|
|
|
|
if (isNil(match)) {
|
|
|
|
throw new InvalidArgumentError('invalid regexp');
|
|
|
|
}
|
|
|
|
const [/* input */, expr, flags] = Array.from(match);
|
|
|
|
return new RegExp(expr, flags);
|
|
|
|
},
|
|
|
|
});
|