precis-js
Version:
A JavaScript implementation of RFC 7564 (The PRECIS Framework).
43 lines (32 loc) • 1.4 kB
text/coffeescript
precis = require './constants'
{ucs2} = require 'punycode'
module.exports = class PrecisEnforcer
constructor: (
) ->
enforce: (profile, string) ->
codepoints = .prepare profile, string
if typeof profile.mapWidth is 'function'
profile.mapWidth codepoints, @
else if profile.widthMapping is precis.WIDTH_MAPPING.EAW
.map codepoints
if typeof profile.map is 'function'
profile.map codepoints, @
if typeof profile.mapCase is 'function'
profile.mapCase codepoints, @
else if profile.caseMapping is precis.CASE_MAPPING.LOWERCASE
codepoints = ucs2.decode ucs2.encode(codepoints).toLowerCase()
if typeof profile.normalize is 'function'
profile.normalize codepoints, @
else
codepoints = .normalize profile.normalization, codepoints
if typeof profile.validateDirectionality is 'function'
profile.validateDirectionality codepoints, @
else if profile.directionality is precis.DIRECTIONALITY.BIDI
.validate codepoints
profile.validate codepoints, @ if typeof profile.validate is 'function'
ucs2.encode codepoints