Skip to content

Instantly share code, notes, and snippets.

@polytypic
Last active November 20, 2016 11:23
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 polytypic/5f728df01ce0f11ae102c23fca9ae0dd to your computer and use it in GitHub Desktop.
Save polytypic/5f728df01ce0f11ae102c23fca9ae0dd to your computer and use it in GitHub Desktop.
Finnish language hyphenation hack with React.
import * as R from "ramda"
import React from "react"
// Rules from http://www.cse.tkk.fi/fi/opinnot/CSE-A1121/English2015/harjoitukset/kierros_2/harj_1/index.html
// Nope, I haven't rigorously checked that I implemented the rules correctly.
const consonantRule =
R.replace(/([aeiouyäö])([b-df-hj-np-tvwxz]*)([b-df-hj-np-t-tvwxz])([aeiouyäö])/gi,
"$1$2\xAD$3$4")
const vowelREs =
[/([^aeiouyäö])(a)([eoyäö])/gi,
/([^aeiouyäö])(e)([aoäö])/gi,
/([^aeiouyäö])(i)([aoyäö])/gi,
/([^aeiouyäö])(o)([aeyäö])/gi,
/([^aeiouyäö])(u)([aeyäö])/gi,
/([^aeiouyäö])(y)([aeouä])/gi,
/([^aeiouyäö])(ä)([aeouö])/gi,
/([^aeiouyäö])(ö)([aeouä])/gi]
const vowelRule = txt =>
R.reduce((txt, re) => R.replace(re, "$1$2\xAD$3", txt), txt, vowelREs)
const diphtongRule =
R.replace(/([^aeiouyäö])(aa|ee|ii|oo|uu|yy|ää|öö|[aeouyäö]i|au|eu|ey|ie|iu|ou|uo|yö|äy|öy)([aeiouyäö])/gi,
"$1$2\xAD$3")
const hyphenate = R.memoize(R.pipe(consonantRule,
consonantRule,
vowelRule,
diphtongRule))
const hyphenated = Elem => ({children, ...props}) =>
<Elem {...props}>
{React.Children.map(children, c =>
typeof c === "string" ? hyphenate(c) : c)}
</Elem>
export const b = hyphenated("b")
export const div = hyphenated("div")
export const h1 = hyphenated("h1")
export const h2 = hyphenated("h2")
export const h3 = hyphenated("h3")
export const h4 = hyphenated("h4")
export const li = hyphenated("li")
export const p = hyphenated("p")
export const span = hyphenated("span")
// ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment