Gathering detailed insights and metrics for @tannin/evaluate
Gathering detailed insights and metrics for @tannin/evaluate
Gathering detailed insights and metrics for @tannin/evaluate
Gathering detailed insights and metrics for @tannin/evaluate
npm install @tannin/evaluate
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
13 Stars
123 Commits
5 Forks
4 Watching
3 Branches
4 Contributors
Updated on 22 Jun 2024
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
-9.7%
11,643
Compared to previous day
Last week
-1.8%
66,179
Compared to previous week
Last month
7.7%
280,550
Compared to previous month
Last year
50.7%
3,108,246
Compared to previous year
No dependencies detected.
Tannin is a gettext localization library.
Inspired by Jed, it is built to be largely compatible with Jed-formatted locale data, and even offers a Jed drop-in replacement compatibility shim to easily convert an existing project. Contrasted with Jed, it is more heavily optimized for performance and bundle size. While Jed works well with one-off translations, it suffers in single-page applications with repeated rendering of elements. Using Tannin, you can expect a bundle size 20% that of Jed (984 bytes gzipped) and upwards of 330x better performance (see benchmarks). It does so without sacrificing the safety of plural forms evaluation, using a hand-crafted expression parser in place of the verbose compiled grammar included in Jed.
Furthermore, the project is architected as a mono-repo, published on npm under the @tannin
scope. These modules can be used standalone, with or without Tannin. For example, you may find value in @tannin/compile
for creating an expression evaluator, or @tannin/sprintf
as a minimal printf string formatter.
The following modules are available:
tannin
@tannin/compat
@tannin/compile
@tannin/evaluate
@tannin/plural-forms
@tannin/postfix
@tannin/sprintf
Using npm as a package manager:
npm install tannin
Otherwise, download a pre-built copy from unpkg:
https://unpkg.com/tannin/dist/tannin.min.js
Construct a new instance of Tannin
, passing locale data in the form of a Jed-formatted JSON object.
The returned Tannin
instance includes the fully-qualified dcnpgettext
function to retrieve a translated string.
1import Tannin from 'tannin'; 2 3const i18n = new Tannin({ 4 the_domain: { 5 '': { 6 domain: 'the_domain', 7 lang: 'en', 8 plural_forms: 'nplurals=2; plural=(n != 1);', 9 }, 10 example: ['singular translation', 'plural translation'], 11 }, 12}); 13 14i18n.dcnpgettext('the_domain', undefined, 'example'); 15// ⇒ 'singular translation'
Tannin accepts plural_forms
both as a standard gettext plural forms string or as a function which, given a number, should return the (zero-based) plural form index. Providing plural_forms
as a function can yield a performance gain of approximately 8x for plural evaluation.
For example, consider the following "default" English (untranslated) initialization:
1const i18n = new Tannin({ 2 messages: { 3 '': { 4 domain: 'messages', 5 plural_forms: (n) => (n === 1 ? 0 : 1), 6 }, 7 }, 8}); 9 10i18n.dcnpgettext('messages', undefined, 'example', 'examples', 1); 11// ⇒ 'example' 12 13i18n.dcnpgettext('messages', undefined, 'example', 'examples', 2); 14// ⇒ 'examples'
For a more human-friendly API, or to more easily transition an existing project, consider using @tannin/compat
as a drop-in replacement for Jed.
1import Jed from '@tannin/compat'; 2 3const i18n = new Jed({ 4 locale_data: { 5 the_domain: { 6 '': { 7 domain: 'the_domain', 8 lang: 'en', 9 plural_forms: 'nplurals=2; plural=(n != 1);', 10 }, 11 example: ['singular translation', 'plural translation'], 12 }, 13 }, 14 domain: 'the_domain', 15}); 16 17i18n.translate('example').fetch(); 18// ⇒ 'singular translation'
The following benchmarks are performed in Node 10.16.0 on a MacBook Pro (2019), 2.4 GHz 8-Core Intel Core i9, 32 GB 2400 MHz DDR4 RAM.
Singular
---
Tannin x 216,670,213 ops/sec ±0.73% (90 runs sampled)
Tannin (Optimized Default) x 219,477,869 ops/sec ±0.32% (96 runs sampled)
Jed x 58,730,499 ops/sec ±0.34% (96 runs sampled)
Singular (Untranslated)
---
Tannin x 75,835,743 ops/sec ±1.26% (96 runs sampled)
Tannin (Optimized Default) x 76,474,169 ops/sec ±0.61% (92 runs sampled)
Jed x 241,632 ops/sec ±0.73% (96 runs sampled)
Plural
---
Tannin x 7,108,006 ops/sec ±0.96% (95 runs sampled)
Tannin (Optimized Default) x 51,658,190 ops/sec ±1.25% (94 runs sampled)
Jed x 236,797 ops/sec ±0.98% (97 runs sampled)
To run benchmarks on your own machine:
git clone https://github.com/aduth/tannin.git
cd tannin
npm install
node packages/tannin/benchmark
Copyright 2019-2024 Andrew Duthie
Released under the MIT License.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
4 existing vulnerabilities detected
Details
Reason
dependency not pinned by hash detected -- score normalized to 2
Details
Reason
Found 1/29 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More