Updated names and made name replacement more reliable
This commit is contained in:
parent
673772385d
commit
17061ea754
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
36
index.js
36
index.js
@ -2,9 +2,7 @@
|
|||||||
const seedrandom = require('seedrandom');
|
const seedrandom = require('seedrandom');
|
||||||
const plotto = require("./data/plotto.json");
|
const plotto = require("./data/plotto.json");
|
||||||
|
|
||||||
function regEscape(s) {
|
function regEscape(s) { return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'); };
|
||||||
return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
|
|
||||||
};
|
|
||||||
|
|
||||||
//seedrandom doesn't expose the seed used, so generate one here
|
//seedrandom doesn't expose the seed used, so generate one here
|
||||||
const defaultSeed = seedrandom()(); ;
|
const defaultSeed = seedrandom()(); ;
|
||||||
@ -194,6 +192,18 @@ class PlotGenerator {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_transformToRegex(o) {
|
||||||
|
let keys = Object.keys(o);
|
||||||
|
keys.sort(function(a,b){return b.length-a.length;});
|
||||||
|
|
||||||
|
if(keys.length == 0) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
let pattern = '\\b(?:' + keys.map(function(s) { return '(?:' + regEscape(s) + ')'; }).join('|') + ')(?![a-z])';
|
||||||
|
return new RegExp(pattern, 'g');
|
||||||
|
}
|
||||||
|
|
||||||
_applyNames(text, cast) {
|
_applyNames(text, cast) {
|
||||||
|
|
||||||
//reset default name lists
|
//reset default name lists
|
||||||
@ -204,13 +214,8 @@ class PlotGenerator {
|
|||||||
|
|
||||||
let nameCache = {};
|
let nameCache = {};
|
||||||
|
|
||||||
let ks = Object.keys(plotto.characters);
|
let rg = this._transformToRegex(plotto.characters);
|
||||||
if(ks.length > 0) {
|
if(rg) {
|
||||||
let pattern = '\\b(?:' + ks.map(function(s) {
|
|
||||||
return '(?:' + regEscape(s) + ')';
|
|
||||||
}).join('|') + ')(?=^|$| |,|\\.)';
|
|
||||||
|
|
||||||
let rg = new RegExp(pattern, 'g');
|
|
||||||
|
|
||||||
text = text.replace(rg,(match) => {
|
text = text.replace(rg,(match) => {
|
||||||
if(!match || match.length == 0)
|
if(!match || match.length == 0)
|
||||||
@ -302,13 +307,8 @@ class PlotGenerator {
|
|||||||
let str = ret.join('\n\n').trim();
|
let str = ret.join('\n\n').trim();
|
||||||
|
|
||||||
if(transform) {
|
if(transform) {
|
||||||
let ks = Object.keys(transform);
|
let rg = this._transformToRegex(transform);
|
||||||
if(ks.length > 0) {
|
if(rg) {
|
||||||
let pattern = '\\b(?:' + ks.map(function(s) {
|
|
||||||
return '(?:' + regEscape(s) + ')';
|
|
||||||
}).join('|') + ')(?=^|$| |,|\\.)';
|
|
||||||
|
|
||||||
let rg = new RegExp(pattern, 'g');
|
|
||||||
|
|
||||||
let cmp = {
|
let cmp = {
|
||||||
before: str,
|
before: str,
|
||||||
@ -337,3 +337,5 @@ class PlotGenerator {
|
|||||||
|
|
||||||
module.exports = PlotGenerator;
|
module.exports = PlotGenerator;
|
||||||
module.exports.defaultSeed = defaultSeed;
|
module.exports.defaultSeed = defaultSeed;
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user