
Security News
ECMAScript 2025 Finalized with Iterator Helpers, Set Methods, RegExp.escape, and More
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
@jsamr/counter-style
Advanced tools
@jsamr/counter-style is an npm package that allows you to create and use custom counter styles in JavaScript. It is particularly useful for generating custom list markers in web applications, providing a flexible way to define and apply various counter styles.
Creating a Custom Counter Style
This feature allows you to create a custom counter style by specifying the type, symbols, and suffix. The example demonstrates creating a numeric counter style with custom symbols and a suffix.
const { counterStyle } = require('@jsamr/counter-style');
const myCustomStyle = counterStyle({
type: 'numeric',
symbols: ['A', 'B', 'C', 'D', 'E'],
suffix: ') '
});
console.log(myCustomStyle.renderMarker(1)); // Output: 'A) '
console.log(myCustomStyle.renderMarker(2)); // Output: 'B) '
Using Predefined Counter Styles
This feature provides access to predefined counter styles such as decimal and lowerAlpha. The example shows how to use these predefined styles to render markers.
const { decimal, lowerAlpha } = require('@jsamr/counter-style/presets');
console.log(decimal.renderMarker(1)); // Output: '1'
console.log(lowerAlpha.renderMarker(1)); // Output: 'a'
Customizing Existing Counter Styles
This feature allows you to customize existing counter styles by modifying their properties. The example demonstrates adding a suffix to the decimal counter style.
const { decimal } = require('@jsamr/counter-style/presets');
const customDecimal = decimal.withSuffix('.');
console.log(customDecimal.renderMarker(1)); // Output: '1.'
console.log(customDecimal.renderMarker(2)); // Output: '2.'
A slim CSS Counter Styles Level 3 compliant library with 47 presets including
Arabic, Persian, Thai, Hebrew, Roman, Katana...
The core is less than 1.7kB minified and gzipped.
Each preset is distributed as a separate module.
Available in both CommonJS and ECMAScript modules.
Targets ECMAScript 2015.
Optimized for metro (React Native) and Webpack bundlers.
Based on prior work from Whang Shuwei.
npm add --save @jsamr/counter-style
yarn add @jsamr/counter-style
This library exports 47 presets. Find your preset here. Each preset is accessible in a separate module to limit bundle size.
import arabicIndic from '@jsamr/counter-style/presets/arabicIndic';
expect(arabicIndic.renderMarker(78)).toBe('٧٨. ');
PRs are welcomed to support other presets. Very easy to implement thanks to this W3C resource.
The API follows closely the specs for CSS @counter-style
at rule. The default export (CounterStyle) is a static object with methods to build CounterStyleRenderer.
In the
below example, we're using the alphabetic counter system and alphabeticFromUnicodeRange
builder which allows to specify a contiguous unicode range. For non-contiguous ranges, use the alphabetic
builder.
import CounterStyle from '@jsamr/counter-style';
const lowerRussian = CounterStyle.alphabeticFromUnicodeRange(
0x430, // а
28
).withSuffix(') ');
// Expect comes from jest testing framework.
// Just a showcase of expected returned values.
expect(lowerRussian.renderCounter(1)).toBe('а');
expect(lowerRussian.renderMarker(1)).toBe('а) ');
expect(lowerRussian.renderCounter(2)).toBe('б');
expect(lowerRussian.renderCounter(3)).toBe('в');
expect(lowerRussian.renderMarker(4)).toBe('г) ');
expect(lowerRussian.renderMarker(5)).toBe('д) ');
expect(lowerRussian.renderCounter(29)).toBe('аа');
expect(lowerRussian.maxMarkerLenInRange(1, 5)).toBe(3);
expect(lowerRussian.maxCounterLenInRange(1, 5)).toBe(1);
Reference: W3C Ready-made Counter Styles: Cyrillic styles.
In the
below example, we're using the symbolic counter system.
Note that withSuffix(null)
removes default suffix.
import CounterStyle from '@jsamr/counter-style';
// Default suffix is ". ", as per the specs.
const funky = CounterStyle.symbolic('*', '&').withSuffix(null);
// Expect comes from jest testing framework.
// Just a showcase of expected returned values.
expect(funky.renderMarker(1)).toBe('*');
expect(funky.renderMarker(2)).toBe('&');
expect(funky.renderMarker(3)).toBe('**');
expect(funky.renderMarker(4)).toBe('&&');
expect(funky.renderMarker(5)).toBe('***');
expect(funky.maxMarkerLenInRange(1, 5)).toBe(3);
expect(funky.maxCounterLenInRange(1, 5)).toBe(3);
All renderers can be chained to create variants, such as withSuffix
,
withPaddingLeft
, ... See available methods in the docs.
The API reference is available here.
Text
elements get trimmed of normal space characters.withMaxLenComputer
.withPadding
) and negative (withNegative
) symbols, one
UTF-16 code unit per symbol must be used. Otherwise, length computation will
be erroneous.speakAs
hasn't been implemented yet.FAQs
CSS Counter Styles Level 3 and presets
The npm package @jsamr/counter-style receives a total of 159,873 weekly downloads. As such, @jsamr/counter-style popularity was classified as popular.
We found that @jsamr/counter-style demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
Security News
A new Node.js homepage button linking to paid support for EOL versions has sparked a heated discussion among contributors and the wider community.
Research
North Korean threat actors linked to the Contagious Interview campaign return with 35 new malicious npm packages using a stealthy multi-stage malware loader.