source: imaps-frontend/node_modules/eslint/lib/shared/string-utils.js

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 1.6 KB
Line 
1/**
2 * @fileoverview Utilities to operate on strings.
3 * @author Stephen Wade
4 */
5
6"use strict";
7
8//------------------------------------------------------------------------------
9// Requirements
10//------------------------------------------------------------------------------
11
12const Graphemer = require("graphemer").default;
13
14//------------------------------------------------------------------------------
15// Helpers
16//------------------------------------------------------------------------------
17
18// eslint-disable-next-line no-control-regex -- intentionally including control characters
19const ASCII_REGEX = /^[\u0000-\u007f]*$/u;
20
21/** @type {Graphemer | undefined} */
22let splitter;
23
24//------------------------------------------------------------------------------
25// Public Interface
26//------------------------------------------------------------------------------
27
28/**
29 * Converts the first letter of a string to uppercase.
30 * @param {string} string The string to operate on
31 * @returns {string} The converted string
32 */
33function upperCaseFirst(string) {
34 if (string.length <= 1) {
35 return string.toUpperCase();
36 }
37 return string[0].toUpperCase() + string.slice(1);
38}
39
40/**
41 * Counts graphemes in a given string.
42 * @param {string} value A string to count graphemes.
43 * @returns {number} The number of graphemes in `value`.
44 */
45function getGraphemeCount(value) {
46 if (ASCII_REGEX.test(value)) {
47 return value.length;
48 }
49
50 if (!splitter) {
51 splitter = new Graphemer();
52 }
53
54 return splitter.countGraphemes(value);
55}
56
57module.exports = {
58 upperCaseFirst,
59 getGraphemeCount
60};
Note: See TracBrowser for help on using the repository browser.