precis-js
Version:
A JavaScript implementation of RFC 7564 (The PRECIS Framework).
80 lines (68 loc) • 2.63 kB
JavaScript
// Generated by CoffeeScript 1.10.0
(function() {
var InvalidCodepointError, PrecisPreparer, precis, ucs2;
ucs2 = require('punycode').ucs2;
InvalidCodepointError = require('./error/InvalidCodepointError');
precis = require('./constants');
module.exports = PrecisPreparer = (function() {
function PrecisPreparer(propertyReader, widthMapper) {
this.propertyReader = propertyReader;
this.widthMapper = widthMapper;
}
PrecisPreparer.prototype.prepare = function(profile, string) {
var codepoints;
if (typeof profile.prepare === 'function') {
return profile.prepare(string, this);
}
codepoints = ucs2.decode(string);
if (typeof profile.prePrepareMap === 'function') {
profile.prePrepareMap(codepoints, this);
}
this.validateStringClass(profile.stringClass, codepoints);
return codepoints;
};
PrecisPreparer.prototype.validateStringClass = function(stringClass, codepoints) {
switch (stringClass) {
case precis.STRING_CLASS.FREEFORM:
this._freeform(codepoints);
break;
case precis.STRING_CLASS.IDENTIFIER:
this._identifier(codepoints);
break;
default:
throw new Error('PRECIS string class not implemented.');
}
return codepoints;
};
PrecisPreparer.prototype._freeform = function(codepoints) {
var category, codepoint, i, isValid, len, results;
results = [];
for (i = 0, len = codepoints.length; i < len; i++) {
codepoint = codepoints[i];
category = this.propertyReader.precisCategory(codepoint);
isValid = category === precis.PRECIS_CATEGORY.PVALID || category === precis.PRECIS_CATEGORY.FREE_PVAL;
if (!isValid) {
throw new InvalidCodepointError("The codepoint " + codepoint + " is not allowed in the 'FreeformClass' string class.");
} else {
results.push(void 0);
}
}
return results;
};
PrecisPreparer.prototype._identifier = function(codepoints) {
var category, codepoint, i, len, results;
results = [];
for (i = 0, len = codepoints.length; i < len; i++) {
codepoint = codepoints[i];
category = this.propertyReader.precisCategory(codepoint);
if (category !== precis.PRECIS_CATEGORY.PVALID) {
throw new InvalidCodepointError("The codepoint " + codepoint + " is not allowed in the 'IdentifierClass' string class.");
} else {
results.push(void 0);
}
}
return results;
};
return PrecisPreparer;
})();
}).call(this);