Giter Site home page Giter Site logo

barbwire-1 / grapheme-counter Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 43 KB

Lightweight (1.6Kb) unicode relying grapheme cluster and emoji counter

License: MIT License

JavaScript 87.38% HTML 12.62%
diacritical-marks grapheme-cluster string-length unicode composed-emojis

grapheme-counter's Introduction

Grapheme-Counter

Grapheme-Counter is a lighweight JavaScript library (1.6Kb!) that provides functions for giving the "correct" length of any strings by counting grapheme clusters, accounting for combined emoji sequences and diacritical marks. It offers two main methods of usage: as a standalone function or by extending the String.prototype with a count property.


Installation

You can install Grapheme-Counter via npm:

npm install grapheme-counter

Alternatively, you can directly include the GraphemeCounter.js file in your project.


API

  • initStringCountProperty() Initializes the count property on the String.prototype, allowing direct usage on strings.

  • countCharacters(str) Counts the number of grapheme clusters in the input string str, accounting for combined emoji sequences and diacritical marks.

  • str (String) The input string to count grapheme clusters in. Returns an object with the following properties:

  • count (Number) The count of grapheme clusters in the input string. Example-Counts

Usage

In vanilla JS - copy the GraphemeCounter.js into your project. In node require the package.

Using String.prototype.count Property

// Import and initialize the count property on String.prototype
import { initStringCountProperty } from 'GraphemeCounter.js';
initStringCountProperty();

// Use the count property directly on strings
const text = 'A flag πŸ³οΈβ€πŸŒˆ!!!';
console.log(text.count); // Output: 11

Using countCharacters Function-return ({count})

// Import the countCharacters function
import { countCharacters } from 'GraphemeCounter.js';

// Count grapheme clusters in a string
const text = 'A flag πŸ³οΈβ€πŸŒˆ!!!';
const count = countCharacters(text).count;
console.log('Grapheme cluster count:', count); // Output: 11

Examples

Click to view examples

    const examples = {
        Greek: "ΓΡιά σου ΞšΟŒΟƒΞΌΞ΅",// 14
        Korean: "μ•ˆλ…•ν•˜μ„Έμš” 세상",// 8
        Hebrew: "Χ©ΧœΧ•Χ Χ’Χ•ΧœΧ",// 9
        Arabic: "Ω…Ψ±Ψ­Ψ¨Ψ§ Ψ¨Ψ§Ω„ΨΉΨ§Ω„Ω…",// 13
        Russian: "ΠŸΡ€ΠΈΠ²Π΅Ρ‚, ΠΌΠΈΡ€",// 11
        Japanese: "γ“γ‚“γ«γ‘γ―γ€δΈ–η•Œ",// 8
        Chinese: "δ½ ε₯½οΌŒδΈ–η•Œ",// 5
        RainbowFlag: "πŸ³οΈβ€πŸŒˆ",// 1
        Family: "πŸ‘¨β€πŸ‘©β€πŸ‘§β€πŸ‘¦πŸ‘¨β€πŸ‘¨β€πŸ‘§β€πŸ‘§πŸ‘©β€πŸ‘©β€πŸ‘¦β€πŸ‘¦πŸ‘©β€πŸ‘§β€πŸ‘§πŸ‘¨β€πŸ‘¦β€πŸ‘¦",// 5
        Man: "πŸ‘¨β€",// 1
        Text: "tex  t",// 6
        Click: "Μ°Click",// 5
        Gujarati: "ΰͺœΰ«‚ΰͺ¨ΰ«€ ΰͺ—ુΰͺœΰͺ°ΰͺΎΰͺ€ΰ«€",// 7
        Sinhala: "ΰΆ―ΰΆ±ΰ·ŠΰΆ±ΰ·€ΰ·",//
        Dream: "rΓͺve",// 4
        Math: "βˆ€x ∈ ℝ: βˆ‘(x_i) = ∫(f(x) dx)",// 27
        EgyptianHieroglyphs: "π“‡“π“π“ˆ–π“‚‹ π“†£π“‘π“†“π“Ž›",// 9
        WavingIntlFlag1:"\ud83c\udff4\udb40\udc67\udb40\udc62\udb40\udc65\udb40\udc6e\udb40\udc67\udb40\udc7f",// 1
        WavingIntlFlag2:"🏴",// 1
        BlackFlag: "🏴",// 1
        testSinhala: "࢐" // 1
    };

    for (const [language, str] of Object.entries(examples)) {
         console.log(str.count);
    }

  

Compatibility

  • Requires ECMAScript 6 (ES6) support.
  • Compatible with modern browsers and environments that support Unicode regular expressions.

License

Grapheme Counter is licensed under the MIT License.

Reporting Issues

If you encounter any bugs, have suggestions for improvements, or would like to request new features, please open an issue on GitHub. When reporting issues, please include detailed information such as:

  • Steps to reproduce the issue
  • Expected behavior
  • Actual behavior
  • Any error messages or console logs

Your feedback is valuable and helps improve the quality of the project. Thank you for contributing!

Contributing

We welcome contributions from the community! If you'd like to contribute to this project, please follow these guidelines:

  1. Fork the repository and clone it to your local machine.
  2. Create a new branch for your changes (git checkout -b feature/your-feature-name).
  3. Make your changes and ensure they follow the project's coding style and conventions.
  4. Write tests for your changes if applicable (I know, I didn't).
  5. Commit your changes with a clear and descriptive message.
  6. Push your changes to your fork (git push origin feature/your-feature-name).
  7. Submit a pull request to the main repository's main branch.

Please be respectful and constructive in your interactions with others. We appreciate your contributions and look forward to working with you!

grapheme-counter's People

Contributors

barbwire-1 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.