voca
Version:
The ultimate JavaScript string library
47 lines (39 loc) • 1.39 kB
JavaScript
;
require('./internal/is_nil.js');
require('./is_string.js');
var coerce_to_string = require('./internal/coerce_to_string.js');
var _const = require('./internal/const.js');
/**
* Reverses the `subject` taking care of
* <a href="https://rainsoft.io/what-every-javascript-developer-should-know-about-unicode/#24surrogatepairs">surrogate pairs</a> and
* <a href="https://rainsoft.io/what-every-javascript-developer-should-know-about-unicode/#25combiningmarks">combining marks</a>.
*
* @function reverseGrapheme
* @static
* @since 1.0.0
* @memberOf Manipulate
* @param {string} [subject=''] The string to reverse.
* @return {string} Returns the reversed string.
* @example
* v.reverseGrapheme('summer');
* // => 'remmus'
*
* v.reverseGrapheme('𝌆 bar mañana mañana');
* // => 'anañam anañam rab 𝌆'
*/
function reverseGrapheme(subject) {
var subjectString = coerce_to_string.coerceToString(subject);
/**
* @see https://github.com/mathiasbynens/esrever
*/
subjectString = subjectString.replace(_const.REGEXP_COMBINING_MARKS, function ($0, $1, $2) {
return reverseGrapheme($2) + $1;
}).replace(_const.REGEXP_SURROGATE_PAIRS, '$2$1');
var reversedString = '';
var index = subjectString.length;
while (index--) {
reversedString += subjectString.charAt(index);
}
return reversedString;
}
module.exports = reverseGrapheme;