Skip to content

Instantly share code, notes, and snippets.

@livewing
Created June 2, 2018 16:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save livewing/e62b7961e14a62ff81f226327b059eff to your computer and use it in GitHub Desktop.
Save livewing/e62b7961e14a62ff81f226327b059eff to your computer and use it in GitHub Desktop.
// mano.js
// JavaScript implementation of
// ManoTranslator (https://github.com/para7/ManoTranslator)
//
// IE以外だったら動く。Nodeでも動くはず。Babelすればどうにでもなりそう
//
// This script is licensed under the MIT License.
//
// Copyright 2018 りぶ
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
'use strict';
(m => (typeof exports === 'object' ? module.exports = m() : window.ManoTranslator = m()))(() => {
const dict = {12413: '000000000', 12422: '000000001', 12372: '00000001', 12397: '0000001', 12419: '000001', 12400: '0000100', 12408: '000010100', 12382: '000010101', 12407: '00001011', 12428: '000011', 12392: '00010', 12369: '000110', 12429: '0001110', 12374: '000111100', 12389: '0001111010', 12355: '0001111011', 12380: '00011111', 12427: '00100', 12418: '001010', 12376: '001011', 12391: '00110', 12421: '0011100', 12434: '0011101', 12431: '001111', 12540: '010000', 12415: '0100010', 12417: '0100011', 12354: '010010', 12423: '010011', 12435: '0101', 12384: '011000', 12359: '011001000', 12412: '011001001', 12402: '01100101', 12368: '01100110', 12353: '011001110', 12403: '011001111', 12387: '01101', 12371: '011100', 12426: '011101', 12399: '011110', 12366: '01111100', 12370: '01111101', 12420: '0111111', 12383: '10000', 12398: '10001', 12394: '10010', 12388: '100110', 12379: '1001110', 12416: '10011110', 12410: '10011111000', 12361: '100111110010', 12386: '1001111100110', 12357: '1001111100111', 12396: '10011111010', 12404: '10011111011', 12401: '100111111', 12356: '1010', 12390: '10110', 12385: '101110', 12360: '1011110', 12381: '1011111', 12375: '11000', 12424: '1100100', 12406: '11001010', 12411: '11001011', 12425: '110011', 12405: '11010000', 12378: '110100010', 12409: '110100011', 12362: '1101001', 12395: '110101', 12365: '110110', 12414: '110111', 12358: '11100', 12364: '111010', 12377: '111011', 12373: '1111000', 12393: '1111001', 12367: '111101', 12363: '11111'};
const rdict = (() => {
const d = {};
Object.keys(dict).forEach(k => d[dict[k]] = String.fromCodePoint(k));
return d;
})();
return {
encode(str) {
return [...str].map(c => {
const cp = c.codePointAt(0);
return dict[cp] ? dict[cp].replace(/0/g, 'ほわっ').replace(/1/g, 'むんっ') : c;
}).join('');
},
decode(str) {
let ret = '';
let code = '';
let f = false;
while (str.length !== 0) {
if (/^ほわっ/.test(str)) {
code += '0';
str = str.slice(3);
f = true;
} else if (/^むんっ/.test(str)) {
code += '1';
str = str.slice(3);
f = true;
}
if (code.length !== 0 && rdict[code]) {
ret += rdict[code];
code = '';
}
if (!f && str[0]) {
ret += code.replace(/0/g, 'ほわっ').replace(/1/g, 'むんっ');
code = '';
ret += str[0];
str = str.slice(1);
}
f = false;
}
return ret;
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment