npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@new-inventor/pluralization

v1.0.1

Published

Smart pluralization for words and templates can be extended for any language

Downloads

28

Readme

JavaScript and TypeScript Smart Pluralisation

Overview

This module provides extendable service for pluralize words.

Instead of "zero | one | more" notation, we provide endings notation.

In the code do something like this {{'{n} {билет|genitive}' | pluralizeTemplate(value)}} or this {{value}} {{'билет' | pluralize(value, 'genitive')}}

This paradigm make code cleaner and make pluralization config slimmer.

Installation

npm install @new-inventor/smart_pluralization

Usage

To use this library in stand alone(not framework) environment you can use this code:

//Initialize service in the beginning of the code
import SmartPluralizationService from '@new-inventor/pluralization/SmartPluralization.service';
import {LocaleName, RUModifier, RUWord} from '@new-inventor/pluralization/locales';
import RU from '@new-inventor/pluralization/locales/RU';
import WordsList from '@new-inventor/pluralization/WordsList';
import ruWords from './ru.pluralization.json';

const pluralizationService = SmartPluralizationService.make(LocaleName.RU, {[LocaleName.RU]: new RU(new WordsList<RUWord>(ruWords))});

//somewhere in the code below
pluralizationService.pluralize('билет', 1, RUModifier.DATIVE);
pluralizationService.pluralizeTemplate('{n} {билет|nominative}', 1);


//or you can implement your own locale by extending the Locale class

import Locale from "@new-inventor/pluralization/Locale";
export default class RU extends Locale {
  public readonly rule: Array<(value: number) => boolean> = [
    (value: number): boolean => {
      return value % 10 === 1;
    },
    (value: number): boolean => {
      return value % 10 >= 2;
    },
    (value: number): boolean => {
      return value % 10 >= 5 || value % 10 === 0 || (value > 10 && value < 20);
    },
  ];

  constructor(public words: WordsList<RUWord> = new WordsList<RUWord>()) {
    super();
  }

  public pluralize(word: string, count: number, modifier?: RUModifier): string {
    return word + this.getEnding(count, this.getCaseVariations(word, modifier));
  }

  protected getCaseVariations(word: string, modifier?: RUModifier): string[] {
    const rawWord = this.words.get(word);
    return rawWord.cases[modifier ? modifier : Object.keys(rawWord.cases)[0]];
  }
}

You should provide file with config for pluralization (ruWords in example above) like this:

{
  "билет": {
    "base": "билет",
    "cases": {
      "nominative": ["", "а", "ов"],
      "genitive": ["а", "ов", "ов"],
      "dative": ["у", "ам", "ам"],
      "accusative": ["", "а", "ов"],
      "instrumental": ["ом", "ами", "ами"],
      "prepositional": ["е", "ах", "ах"]
    }
  },
  "подразделение": {
    "base": "подразделени",
      "cases": {
        "nominative": ["е", "я", "й"],
        "genitive": ["я", "й", "й"],
        "dative": ["ю", "ям", "ям"],
        "accusative": ["е", "я", "й"],
        "instrumental": ["ем", "ями", "ями"],
        "prepositional": ["е", "ях", "ях"]
    }
  }
}

this file can be of type json, js and ts.

This Version implement next locales out of the box:

  • EN - english
  • RU - russian

All locales names written in two upper case letters notation.

Vue usage

Use this package: @new-inventor/vue-pluralization

npm install @new-inventor/vue-pluralization

in main.ts/js

import SmartPluralizationPlugin from '@new-inventor/vue-pluralization';
import {LocaleName, RUWord} from '@new-inventor/pluralization/locales';
import RU from '@new-inventor/pluralization/locales/RU';
import WordsList from '@new-inventor/pluralization/WordsList';
import ruWords from './ru.pluralization.json';
//...
Vue.use(SmartPluralizationPlugin, {currentLocale: 'RU', locales: {[LocaleName.RU]: new RU(new WordsList<RUWord>(ruWords))}});

in templates

<div>{{ n }} {{'билет' | pluralize(1, 'nominative')}}</div>
<div>{{ '{n} {билет|nominative}' | pluralizeTemplate(1)}}</div>

in components

this.$pluralizer.pluralize('билет', 1, RUModifier.DATIVE);
this.$pluralizer.pluralizeTemplate('{n} {билет|nominative}', 1);

Angular usage

use this package: @new-inventor/angular-pluralization

npm install @new-inventor/angular-pluralization

import module

@NgModule({
  imports:      [ SmartPluralizationModule ],
  // ...
})

inject in component or service

import SmartPluralizationService from '@new-inventor/angular-pluralization/SmartPluralization.service';

class SomeClass{
  constructor(private pluralizationService: SmartPluralizationService){}
}

use in component

this.pluralizationService.pluralize('билет', 1, RUModifier.DATIVE);
this.pluralizationService.pluralizeTemplate('{n} {билет|nominative}', 1);

use in templates

<div>{{ n }} {{ 'билет' | pluralize:1:'nominative' }}</div>
<div>{{ '{n} {билет|nominative}' | pluralizeTemplate:1 }}</div>

Contribute

Pull requests are always welcome.

Issues you can add here: https://github.com/new-inventor/smart_pluralization/issues

LICENCE

MIT