Skip to content

Instantly share code, notes, and snippets.

@nexpr
Last active May 17, 2021 07:20
Show Gist options
  • Save nexpr/342e4e121acc9c62904070dbdd07db27 to your computer and use it in GitHub Desktop.
Save nexpr/342e4e121acc9c62904070dbdd07db27 to your computer and use it in GitHub Desktop.
縦書き

Example

let text = `
あいう
えお
かきくけこ
`.trim()

console.log(text)
text = vw(text)
console.log(text)
text = vw(text)
console.log(text)
text = vw(text)
console.log(text)
text = vw(text)
console.log(text)

Result

あいう
えお
かきくけこ
かえあ
きおい
く う
け  
こ  
こけくきか
   おえ
  ういあ
  こ
  け
う く
いおき
あえか
あいう  
えお   
かきくけこ
/*
convert
あいう
えお
かきくけこ
to
かえあ
きおい
く う
*/
export const vw = str => {
const lines = str.split("\n")
const rlines = []
for (let c = 0;; c++) {
const chars = []
for (let r = lines.length - 1; r >= 0; r--) {
chars.push(lines[r].codePointAt(c))
}
if (chars.join("") === "") {
return rlines.join("\n")
}
rlines.push(chars.map(x => x !== undefined ? String.fromCodePoint(x) : " ").join(""))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment