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

@bem/sdk.keyset

v0.1.1

Published

Representation of BEM i18n keyset

Downloads

8

Readme

Keyset

The tool for representation of BEM i18n keyset.

NPM Status

Introduction

Keyset representations BEM project's keysets and returns a JavaScript object with information about it.

Try keyset

An example is available in the RunKit editor.

Quick start

Attention. To use @bem/sdk.keyset, you must install Node.js 8.0+.

To run the @bem/sdk.keyset package:

  1. Install keyset.
  2. Declaration keyset.

Installing the @bem/sdk.keyset package

To install the @bem/sdk.keyset package, run the following command:

$ npm install --save @bem/sdk.keyset

Declaration keyset

Specify the Keyset name, path, and format for keyset. The Keyset class is a constructor for classes that enable format-sensitive keyset formatting.

Example:

const { Keyset } = require('@bem/sdk.keyset');
const keyset = new Keyset('Time', 'src/features/Time/Time.i18n');
keyset.name; // => 'Time'.
keyset.path; // => 'src/features/Time/Time.i18n'.
keyset.format; // => 'taburet' — default format, see Formats.

RunKit live editor.

Formats

Keyset has two default formats:

| Format | Extension | |--------|-----------| | enb | .js | | taburet | .ts |

If you want to change default extension, override a variable keyset.langsKeysExt before saving keyset.

Example:

const mockfs = require('mock-fs');
const { Keyset, Key, ParamedKey, PluralKey, LangKeys } = require("@bem/sdk.keyset");

mockfs({
    'src/features/Time/Time.i18n': {}
});

const langKeys = new LangKeys('ru', [
    new Key('Time difference', 'Разница во времени'),
    new PluralKey('{count} minute', {
        one: new ParamedKey('{count} minute', '{count} минута', ['count']),
        some: new ParamedKey('{count} minute', '{count} минуты', ['count']),
        many: new ParamedKey('{count} minute', '{count} минут', ['count']),
        none: new Key('{count} minute', 'нет минут')
    })
]);

const keyset = new Keyset('Time', 'src/features/Time/Time.i18n');
keyset.addKeysForLang('ru', langKeys);
keyset.langsKeysExt = '.ts';
await keyset.save();
keyset;

RunKit live editor.

API reference

keyset.load()

Loads keyset from project's file system.

async keyset.load();

Example:

const mockfs = require('mock-fs');
const { stripIndent } = require('common-tags');
const { Keyset } = require("@bem/sdk.keyset");

mockfs({
    'src/features/Time/Time.i18n': {
        'ru.js': stripIndent`
            export const ru = {
                'Time difference': 'Разница во времени',
                '{count} minute': {
                    'one': '{count} минута',
                    'some': '{count} минуты',
                    'many': '{count} минут',
                    'none': 'нет минут',
                },
            };
        `,
        'en.js': stripIndent`
            export const en = {
                'Time difference': 'Time difference',
                '{count} minute': {
                    'one': '{count} minute',
                    'some': '{count} minutes',
                    'many': '{count} minutes',
                    'none': 'none',
                },
            };
        `
    }
});

const keyset = new Keyset('Time', 'src/features/Time/Time.i18n');
await keyset.load();
keyset.langs; // => ['en', 'ru']

RunKit live editor.

keyset.getLangKeysForLang(lang)

Gets keys from found keyset.

/**
* Gets keys.
*
* @param {string} lang — The language to traverse.
* @return {string[]} — Keys.
*/
keyset.getLangKeysForLang(lang);

Example:

const mockfs = require('mock-fs');
const { stripIndent } = require('common-tags');
const { Keyset } = require("@bem/sdk.keyset");

mockfs({
    'src/features/Time/Time.i18n': {
        'ru.js': stripIndent`
            export const ru = {
                'Time difference': 'Разница во времени',
                '{count} minute': {
                    'one': '{count} минута',
                    'some': '{count} минуты',
                    'many': '{count} минут',
                    'none': 'нет минут',
                },
            };
        `,
        'en.js': stripIndent`
            export const en = {
                'Time difference': 'Time difference',
                '{count} minute': {
                    'one': '{count} minute',
                    'some': '{count} minutes',
                    'many': '{count} minutes',
                    'none': 'none',
                },
            };
        `
    }
});

const keyset = new Keyset('Time', 'src/features/Time/Time.i18n');
await keyset.load();
const langKeys = keyset.getLangKeysForLang('ru');

langKeys.keys; // => [Key {name: 'Time difference', value: 'Разница во времени'}, PluralKey { ... }]

RunKit live editor.

keyset.addKeysForLang(lang, langKeys)

Adds keys for language. Use with keyset.save() method.

/**
* Adds keys.
*
* @param {string} lang — The language to add.
* @return {object[]} — Keys.
*/
keyset.addKeysForLang(lang, langKeys);

Example:

const mockfs = require('mock-fs');
const { Keyset, Key, ParamedKey, PluralKey, LangKeys } = require("@bem/sdk.keyset");

mockfs({
    'src/features/Time/Time.i18n': {}
});

const ruLangKeys = new LangKeys('ru', [
    new Key('Time difference', 'Разница "во" времени'),
    new PluralKey('{count} minute', {
        one: new ParamedKey('{count} minute', '{count} минута', ['count']),
        some: new ParamedKey('{count} minute', '{count} минуты', ['count']),
        many: new ParamedKey('{count} minute', '{count} минут', ['count']),
        none: new Key('{count} minute', 'нет минут')
    })
]);

const enLangKeys = new LangKeys('en', [
    new Key('Time difference', 'Time difference',),
    new PluralKey('{count} minute', {
        one: new ParamedKey('{count} minute', '{count} minute', ['count']),
        some: new ParamedKey('{count} minute', '{count} minutes', ['count']),
        many: new ParamedKey('{count} minute', '{count} minutes', ['count']),
        none: new Key('{count} minute', 'none')
    })
]);

const keyset = new Keyset('Time', 'src/features/Time/Time.i18n');
keyset.addKeysForLang('ru', ruLangKeys);
keyset.addKeysForLang('en', enLangKeys);
await keyset.save();
keyset.langs; // => ['ru', 'en']

RunKit live editor.

keyset.save()

Saves keyset to project's file system. Use with keyset.addKeysForLang(lang, langKeys) method.

async keyset.save();

Example:

const mockfs = require('mock-fs');
const { Keyset, Key, ParamedKey, PluralKey, LangKeys } = require("@bem/sdk.keyset");

mockfs({
    'src/features/Time/Time.i18n': {}
});

const langKeys = new LangKeys('ru', [
    new Key('Time difference', 'Разница во времени'),
    new PluralKey('{count} minute', {
        one: new ParamedKey('{count} minute', '{count} минута', ['count']),
        some: new ParamedKey('{count} minute', '{count} минуты', ['count']),
        many: new ParamedKey('{count} minute', '{count} минут', ['count']),
        none: new Key('{count} minute', 'нет минут')
    })
]);

const keyset = new Keyset('Time', 'src/features/Time/Time.i18n');
keyset.addKeysForLang('ru', langKeys);
await keyset.save();
keyset.langs; // => ['ru']

RunKit live editor.

LangKeys.stringify(value, formatName);

Converts a JavaScript object to a special string ready to save on the project's file system.

/**
 * Converts a JavaScript object to a string.
 *
 * @param {Object} value — The value to convert.
 * @param {string} formatName  — The name of format.
 * @returns {string} — The string to save.
 */
LangKeys.stringify(value, formatName);

Example:

const { Keyset, Key, ParamedKey, PluralKey, LangKeys } = require("@bem/sdk.keyset");
const langKeys = new LangKeys('ru', [
    new Key('Time difference', 'Разница во времени'),
    new PluralKey('{count} minute', {
        one: new ParamedKey('{count} minute', '{count} минута', ['count']),
        some: new ParamedKey('{count} minute', '{count} минуты', ['count']),
        many: new ParamedKey('{count} minute', '{count} минут', ['count']),
        none: new Key('{count} minute', 'нет минут')
    })
]);
langKeys.stringify('taburet');
// => "export const ru = {\n'Time difference': 'Разница "во" времени',\n'{count} minute': {\n'one': '{count} минута',\n'some': '{count} минуты',\n'many': '{count} минут',\n'none': 'нет минут',\n},\n};"

RunKit live editor.

LangKeys.parse(str, formatName)

Parses a string, constructing the JavaScript object described by the string.

/**
 * Parses a string to JavaScript object.
 *
 * @param {Object} str — The string to parse.
 * @param {string} formatName  — The name of format.
 * @returns {string} — The JavaScript object.
 */
await LangKeys.parse(str, formatName);

Example:

const { Keyset, Key, ParamedKey, PluralKey, LangKeys } = require("@bem/sdk.keyset");
const { stripIndent } = require('common-tags');
const str = stripIndent`
    export const ru = {
        'Time difference': 'Разница "во" времени',
        '{count} minute': {
            'one': '{count} минута',
            'some': '{count} минуты',
            'many': '{count} минут',
            'none': 'нет минут',
        },
    };
`;

const langKeys = await LangKeys.parse(str, 'taburet');
langKeys;

RunKit live editor.

License

© 2019 Yandex. Code released under Mozilla Public License 2.0.