voca
Version:
The ultimate JavaScript string library
45 lines (38 loc) • 1.39 kB
JavaScript
import './internal/is_nil.js';
import './is_string.js';
import { c as coerceToString } from './internal/coerce_to_string.js';
import { b as REGEXP_COMBINING_MARKS, c as REGEXP_SURROGATE_PAIRS } from './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 = coerceToString(subject);
/**
* @see https://github.com/mathiasbynens/esrever
*/
subjectString = subjectString.replace(REGEXP_COMBINING_MARKS, function ($0, $1, $2) {
return reverseGrapheme($2) + $1;
}).replace(REGEXP_SURROGATE_PAIRS, '$2$1');
var reversedString = '';
var index = subjectString.length;
while (index--) {
reversedString += subjectString.charAt(index);
}
return reversedString;
}
export default reverseGrapheme;