Skip to content

Commit 62df504

Browse files
iGrozaiGroza
authored and
iGroza
committed
💚 initial commit
0 parents  commit 62df504

16 files changed

+4156
-0
lines changed

.eslintrc.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module.exports = {
2+
parser: '@typescript-eslint/parser',
3+
plugins: ['@typescript-eslint'],
4+
extends: [
5+
'eslint:recommended',
6+
'plugin:@typescript-eslint/recommended',
7+
],
8+
parserOptions: {
9+
ecmaVersion: 2020,
10+
sourceType: 'module',
11+
},
12+
rules: {
13+
// Add your custom rules here
14+
},
15+
};

.github/workflows/npm-publish.yml

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
2+
# For more information see: https://docs.github.com/en/actions/publishing-packages/publishing-nodejs-packages
3+
4+
name: Node.js Package
5+
6+
on:
7+
release:
8+
types: [created]
9+
10+
jobs:
11+
build:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v3
15+
- uses: actions/setup-node@v3
16+
with:
17+
node-version: 16
18+
- run: yarn install --frozen-lockfile
19+
- run: yarn test
20+
21+
publish-npm:
22+
needs: build
23+
runs-on: ubuntu-latest
24+
steps:
25+
- uses: actions/checkout@v3
26+
- uses: actions/setup-node@v3
27+
with:
28+
node-version: 16
29+
registry-url: https://registry.npmjs.org/
30+
- run: yarn install --frozen-lockfile
31+
- run: yarn build
32+
- run: yarn publish --access public
33+
env:
34+
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules/
2+
dist/

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 Haqq Network
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Format Number with Subscript Zeros
2+
3+
A library to format numbers by hiding leading zeros using subscript digits. This method is particularly useful for representing very small numbers in a compact and readable format. The subscript digits indicate the number of hidden zeros.
4+
5+
[Documentation](https://github.com/haqq-network/format-number-with-subscript-zeros/blob/master/docs/globals.md)
6+
7+
## Installation
8+
9+
```bash
10+
yarn add @haqq/format-number-with-subscript-zeros
11+
```
12+
13+
## Usage
14+
15+
```ts
16+
import { formatNumberWithSubscriptZeros } from '@haqq/format-number-with-subscript-zeros';
17+
18+
console.log(formatNumberWithSubscriptZeros("0.42")); // "0.42"
19+
console.log(formatNumberWithSubscriptZeros("0.042")); // "0.042"
20+
console.log(formatNumberWithSubscriptZeros("0.0042")); // "0.0042"
21+
console.log(formatNumberWithSubscriptZeros("0.00042")); // "0.00042"
22+
console.log(formatNumberWithSubscriptZeros("0.000042")); // "0.000042"
23+
console.log(formatNumberWithSubscriptZeros("0.0000042")); // "0.0₅42"
24+
console.log(formatNumberWithSubscriptZeros("0.00000042")); // "0.0₆42"
25+
console.log(formatNumberWithSubscriptZeros("0.000000042")); // "0.0₇42"
26+
console.log(formatNumberWithSubscriptZeros("0.0000000042")); // "0.0₈42"
27+
console.log(formatNumberWithSubscriptZeros("0.00000000042")); // "0.0₉42"
28+
console.log(formatNumberWithSubscriptZeros("0.000000000042")); // "0.0₁₀42"
29+
console.log(formatNumberWithSubscriptZeros("0.0000000000042")); // "0.0₁₁42"
30+
console.log(formatNumberWithSubscriptZeros("0.00000000000042")); // "0.0₁₂42"
31+
```
32+
33+
### Additional Parameters
34+
35+
The formatNumberWithSubscriptZeros function accepts the following parameters:
36+
37+
- numberStr (string): The number string to format.
38+
- presiction (number, optional): The number of decimal places to include in the formatted string. Default is 3.
39+
- min (number, optional): The minimum value for which the formatting should be applied. Default is 0.00001.
40+
41+
## Running Tests
42+
43+
```sh
44+
yarn test
45+
```
46+
47+
## Building the Project
48+
49+
```sh
50+
yarn build
51+
```
52+
53+
## Linting the Code
54+
55+
```sh
56+
yarn lint
57+
```
58+
59+
## Benefits and Convenience
60+
61+
This method of formatting numbers is particularly useful for:
62+
63+
• Readability: It makes very small numbers easier to read and understand by representing leading zeros as subscript digits.
64+
• Compact Representation: It reduces the length of the number string by using subscript digits for leading zeros.
65+
• Scientific Applications: It is useful in scientific and engineering contexts where numbers can be extremely small and a clear representation is needed.
66+
67+
## How It Works
68+
69+
The function converts the number string to a floating-point number and checks if it is less than 0.01. If the number is larger, it returns the original string. For smaller numbers, it calculates the number of leading zeros and formats them as subscript digits.
70+
71+
## License
72+
73+
MIT

docs/README.md

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
**@haqq/format-number-with-subscript-zeros v1.0.0**[**Docs**](globals.md)
2+
3+
***
4+
5+
# Format Number with Subscript Zeros
6+
7+
A library to format numbers by hiding leading zeros using subscript digits. This method is particularly useful for representing very small numbers in a compact and readable format. The subscript digits indicate the number of hidden zeros.
8+
9+
[Documentation](https://github.com/haqq-network/format-number-with-subscript-zeros/blob/master/docs/globals.md)
10+
11+
## Installation
12+
13+
```bash
14+
yarn add @haqq/format-number-with-subscript-zeros
15+
```
16+
17+
## Usage
18+
19+
```ts
20+
import { formatNumberWithSubscriptZeros } from '@haqq/format-number-with-subscript-zeros';
21+
22+
console.log(formatNumberWithSubscriptZeros("0.42")); // "0.42"
23+
console.log(formatNumberWithSubscriptZeros("0.042")); // "0.042"
24+
console.log(formatNumberWithSubscriptZeros("0.0042")); // "0.0042"
25+
console.log(formatNumberWithSubscriptZeros("0.00042")); // "0.00042"
26+
console.log(formatNumberWithSubscriptZeros("0.000042")); // "0.000042"
27+
console.log(formatNumberWithSubscriptZeros("0.0000042")); // "0.0₅42"
28+
console.log(formatNumberWithSubscriptZeros("0.00000042")); // "0.0₆42"
29+
console.log(formatNumberWithSubscriptZeros("0.000000042")); // "0.0₇42"
30+
console.log(formatNumberWithSubscriptZeros("0.0000000042")); // "0.0₈42"
31+
console.log(formatNumberWithSubscriptZeros("0.00000000042")); // "0.0₉42"
32+
console.log(formatNumberWithSubscriptZeros("0.000000000042")); // "0.0₁₀42"
33+
console.log(formatNumberWithSubscriptZeros("0.0000000000042")); // "0.0₁₁42"
34+
console.log(formatNumberWithSubscriptZeros("0.00000000000042")); // "0.0₁₂42"
35+
```
36+
37+
### Additional Parameters
38+
39+
The formatNumberWithSubscriptZeros function accepts the following parameters:
40+
41+
- numberStr (string): The number string to format.
42+
- presiction (number, optional): The number of decimal places to include in the formatted string. Default is 3.
43+
- min (number, optional): The minimum value for which the formatting should be applied. Default is 0.00001.
44+
45+
## Running Tests
46+
47+
```sh
48+
yarn test
49+
```
50+
51+
## Building the Project
52+
53+
```sh
54+
yarn build
55+
```
56+
57+
## Linting the Code
58+
59+
```sh
60+
yarn lint
61+
```
62+
63+
## Benefits and Convenience
64+
65+
This method of formatting numbers is particularly useful for:
66+
67+
• Readability: It makes very small numbers easier to read and understand by representing leading zeros as subscript digits.
68+
• Compact Representation: It reduces the length of the number string by using subscript digits for leading zeros.
69+
• Scientific Applications: It is useful in scientific and engineering contexts where numbers can be extremely small and a clear representation is needed.
70+
71+
## How It Works
72+
73+
The function converts the number string to a floating-point number and checks if it is less than 0.01. If the number is larger, it returns the original string. For smaller numbers, it calculates the number of leading zeros and formats them as subscript digits.
74+
75+
## License
76+
77+
MIT
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
[**@haqq/format-number-with-subscript-zeros v1.0.0**](../README.md)**Docs**
2+
3+
***
4+
5+
[@haqq/format-number-with-subscript-zeros v1.0.0](../globals.md) / formatNumberWithSubscriptZeros
6+
7+
# Function: formatNumberWithSubscriptZeros()
8+
9+
> **formatNumberWithSubscriptZeros**(`numberStr`, `presiction`, `min`): `string`
10+
11+
Formats a number string using scientific notation with subscript zeros.
12+
13+
## Parameters
14+
15+
**numberStr**: `string`
16+
17+
The number string to format.
18+
19+
**presiction**: `number`= `3`
20+
21+
The number of decimal places to include in the formatted string. Default is 3.
22+
23+
**min**: `number`= `0.00001`
24+
25+
The minimum value for which the formatting should be applied. Default is 0.00001.
26+
27+
## Returns
28+
29+
`string`
30+
31+
The formatted number string.
32+
33+
## Source
34+
35+
[formatNumberWithSubscriptZeros.ts:9](https://github.com/haqq-network/format-number-with-subscript-zeros/blob/df8f6b1745567221a9d0f075968fa0e77abc1479/src/formatNumberWithSubscriptZeros.ts#L9)

docs/globals.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[**@haqq/format-number-with-subscript-zeros v1.0.0**](README.md)**Docs**
2+
3+
***
4+
5+
# @haqq/format-number-with-subscript-zeros v1.0.0
6+
7+
## Functions
8+
9+
- [formatNumberWithSubscriptZeros](functions/formatNumberWithSubscriptZeros.md)

jest.config.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
preset: 'ts-jest',
3+
testEnvironment: 'node',
4+
testMatch: ['**/tests/**/*.test.ts'],
5+
};

package.json

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"name": "@haqq/format-number-with-subscript-zeros",
3+
"version": "1.0.0",
4+
"description": "A library to format numbers using scientific notation with subscript zeros. This method is particularly useful for representing very small numbers in a compact and readable format.",
5+
"main": "dist/index.js",
6+
"scripts": {
7+
"build": "webpack",
8+
"test": "jest",
9+
"lint": "eslint --ext .ts src/",
10+
"prepublishOnly": "yarn build && yarn test && yarn lint",
11+
"typedoc": "typedoc"
12+
},
13+
"repository": {
14+
"type": "git",
15+
"url": "git+https://github.com/haqq-network/format-number-with-subscript-zeros.git"
16+
},
17+
"keywords": [
18+
"format",
19+
"number",
20+
"subscript",
21+
"zeros",
22+
"scientific",
23+
"notation"
24+
],
25+
"author": "iGroza",
26+
"license": "MIT",
27+
"bugs": {
28+
"url": "https://github.com/haqq-network/format-number-with-subscript-zeros/issues"
29+
},
30+
"homepage": "https://github.com/haqq-network/format-number-with-subscript-zeros#readme",
31+
"devDependencies": {
32+
"@types/jest": "^27.0.1",
33+
"@types/node": "^16.0.0",
34+
"@typescript-eslint/eslint-plugin": "^4.0.0",
35+
"@typescript-eslint/parser": "^4.0.0",
36+
"eslint": "^7.0.0",
37+
"jest": "^27.0.0",
38+
"terser-webpack-plugin": "^5.0.0",
39+
"ts-jest": "^27.0.0",
40+
"ts-loader": "^9.5.1",
41+
"typedoc": "^0.25.13",
42+
"typedoc-plugin-markdown": "^4.0.2",
43+
"typescript": "4.4.4",
44+
"webpack": "^5.0.0",
45+
"webpack-cli": "^4.0.0"
46+
}
47+
}

src/formatNumberWithSubscriptZeros.ts

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Formats a number string using scientific notation with subscript zeros.
3+
*
4+
* @param numberStr - The number string to format.
5+
* @param presiction - The number of decimal places to include in the formatted string. Default is 3.
6+
* @param min - The minimum value for which the formatting should be applied. Default is 0.00001.
7+
* @returns The formatted number string.
8+
*/
9+
export function formatNumberWithSubscriptZeros(numberStr: string, presiction = 3, min = 0.00001): string {
10+
const number = parseFloat(numberStr);
11+
if (number >= min) {
12+
const [part0, part1] = numberStr.split('.')
13+
if(part1) {
14+
const leadingZeros = part1?.match?.(/0+/)?.[0] || '';
15+
return `${part0}.${leadingZeros}${part1.replace(leadingZeros, '').slice(0, presiction)}`
16+
}
17+
return part1 ? [part0, part1.slice(0, presiction)].join('.') : part0;
18+
}
19+
20+
const leadingZerosMatch = numberStr.match(/^0\.(0+)/);
21+
if (!leadingZerosMatch) return numberStr;
22+
23+
const leadingZerosCount = leadingZerosMatch[1].length;
24+
const remainingDigits = numberStr.slice(leadingZerosMatch[0].length);
25+
26+
const smallCount = String(leadingZerosCount).split('').map(digit => String.fromCharCode(8320 + parseInt(digit))).join('');
27+
28+
return `0.0${smallCount}${remainingDigits.slice(0, presiction)}`;
29+
}

src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './formatNumberWithSubscriptZeros';

0 commit comments

Comments
 (0)