0.0.0 ā€¢ Published 3 years ago

martis v0.0.0

Weekly downloads
-
License
MIT
Repository
-
Last release
3 years ago

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