UNPKG

ja-greetings

Version:

ja-greetings is a cli tool that displays Japanese greetings

95 lines (74 loc) 2.32 kB
module.exports = (text, opts) => { const lines = text.split('\n') const greeting = '\n' + top_surround(get_length(lines, opts.l), opts.s) + text + bottom_surround(get_length(lines, opts.l), opts.s) return greeting } top_surround = (length, pattern, lang_flg) => { switch (pattern) { case 'no': return '' case 'w-star': return new Array(Math.floor(length/2) + 3).join('☆ ') + '\n' case 'b-star': return new Array(Math.floor(length/2) + 3).join('★ ') + '\n' case 'slash': return new Array(Math.floor(length/3) + 3).join('/ ̄') + '\n' case 'asterisk': return new Array(Math.floor(length/2) + 3).join('*') + '\n' case 'w-tri': return new Array(Math.floor(length/2) + 3).join('▽ ') + '\n' case 'b-tri': return new Array(Math.floor(length/2) + 3).join('▼ ') + '\n' default: return new Array(length + 3).join('-') + '\n' } } bottom_surround = (length, pattern) => { switch (pattern) { case 'no': return '' case 'w-star': return '\n' + new Array(Math.floor(length/2) + 3).join('☆ ') case 'b-star': return '\n' + new Array(Math.floor(length/2) + 3).join('★ ') case 'slash': return '\n' + new Array(Math.floor(length/3) + 3).join('_/') case 'asterisk': return '\n' + new Array(Math.floor(length/2) + 3).join('*') case 'w-tri': return '\n' + new Array(Math.floor(length/2) + 3).join('△ ') case 'b-tri': return '\n' + new Array(Math.floor(length/2) + 3).join('▲ ') default: return '\n' + new Array(length + 3).join('-') } } get_length = (lines, flg) => { let max = lines[0].length if (!flg) return max else for (line of lines) { if (line.length > max) max = line.length // Double-byte character check max += count_characters(line) } return max } count_characters = (text) => { let len = 0 let str = escape(line) for (let i = 0; i < str.length; i++) { if (str.charAt(i) === "%") { if (str.charAt(++i) === "u") { i += 3; len++; } i++; } } return len / 2 }