1
0
Fork 0
mirror of https://github.com/Oreolek/pancakes-or-waffles.git synced 2024-05-17 00:08:25 +03:00
This commit is contained in:
Lau Skeeter 2018-02-10 21:06:34 +01:00
parent e4855d771f
commit c421637878
4 changed files with 65 additions and 54 deletions

View file

@ -13,3 +13,4 @@ gravity-rush
forza
dk64
wii
paper-mario

View file

@ -1,74 +1,58 @@
const expandBracketWords = require('./txt/expandBracketWords.js');
const expandProps = require('./txt/expandProps.js');
const propsRegex = /\((.*?)\)/;
const parse = (str) => {
let array = str.split('\n');
return str.split('\n')
array = array
/*
basic mangling
*/
.map(chunk => chunk.trim())
.filter(chunk => chunk.charAt(0) !== '#')
.filter(chunk => chunk.length > 0)
.map(chunk => chunk === '_empty_'?'':chunk)
/*
expand [the words, the things] between
brackets into all possibilities, then
expand all to fix the probabilities biasing
towards the bracketed ones
*/
.map(expandBracketWords)
.map(chunk =>
(chunk instanceof Array)
? chunk
: [chunk]
);
const repeatCount = array.map(_ => _.length);
const max = repeatCount.reduce((acc,item)=>
item > acc
? item
: acc
,0);
array = array
.map((item,index)=>{
.map((item, index, original) => {
const max = original
.map(_ => _.length)
.reduce((acc,item) =>
item > acc
? item
: acc
, 0 );
while(item.length < max) {
item = [...item, ...item];
}
return item;
})
.reduce((acc,chunk)=>[...acc,...chunk],[])
/*
grab the props specified at the end
*/
.map(expandProps)
.map((chunk,i,original) => {
const propCount = original.reduce((acc,current) =>
acc + Object.keys(current.props).length
, 0);
return propCount < 1
? chunk.value
: chunk;
});
array = [].concat.apply([], array);
const arrayWithProps = array
.map(chunk => {
let props = {};
let propArray = propsRegex.exec(chunk);
if(propArray) {
propArray = propArray[1].split(',');
chunk = chunk.replace(propsRegex,'').trim();
}
else {
propArray = [];
}
if(propArray.length > 0) {
propArray.map(prop => {
prop = prop.split('=');
props[prop[0]] = prop[1]?prop[1]:true;
});
}
return {
value: chunk,
props: props
};
});
const propCount = arrayWithProps.reduce((acc,current) => {
return acc + Object.keys(current.props).length;
}, 0);
if(propCount < 1) {
return arrayWithProps.map(item => item.value);
}
else {
return arrayWithProps;
}
};
module.exports = parse;

View file

@ -32,4 +32,5 @@ module.exports = (chunk) => {
chunk = [chunk];
}
return chunk;
};

View file

@ -0,0 +1,25 @@
const propsRegex = /\((.*?)\)/;
module.exports = (chunk) => {
let props = {};
let propArray = propsRegex.exec(chunk);
if(propArray) {
propArray = propArray[1].split(',');
chunk = chunk.replace(propsRegex,'').trim();
}
else {
propArray = [];
}
if(propArray.length > 0) {
propArray.map(prop => {
prop = prop.split('=');
props[prop[0]] = prop[1]?prop[1]:true;
});
}
return {
value: chunk,
props: props
};
};