0.0.0 ā¢ Published 3 years ago
martis v0.0.0
MARTIS
Markov algorithm TL;DR:
- the rules are a sequence of pairs of strings;
- usually presented in the form of pattern ā replacement;
- each rule may be either ordinary or terminating.
Martis TL;DR:
- same principles as above,
- a bit more utility
- all in JS.
Examples
Binary to unary
const bin2uni = martis(
{
step: ({str}) => console.log(str),
},
[
{
pat: /\|0/,
rep: '0||',
},
{
pat: '1',
rep: '0|',
},
{
pat: '0',
rep: '',
},
],
);
bin2uni('111');
produces
0|11
0|0|1
00|||1
00|||0|
00||0|||
00|0|||||
000|||||||
00|||||||
0|||||||
|||||||
Emojis
const re = ($) => new RegExp($, 'u');
const rule = (pat, rep) => ({pat: re(pat), rep});
const replace = martis(
{
step: ({idx, str}) => void (
console.log(idx, '->', str)
),
},
[
rule(/:\)/, 'š'),
rule(':D', 'š'),
rule('x', 'š'),
rule('šš', 'š¤£'),
rule('šš¤£', 'š'),
],
);
console.log(replace(':):)x'));
produces
1n -> š:)x
2n -> ššx
3n -> ššš
4n -> šš¤£
5n -> š
š
Lower case
const lower = martis(
[{
end: true,
pat: '.*',
rep: ({old}) => old.toLowerCase(),
}],
);
console.log(lower('AxByCzD'));
produces
axbyczd
0.0.0
3 years ago