Skip to content

Commit

Permalink
💚 initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
iGroza authored and iGroza committed May 27, 2024
0 parents commit 62df504
Show file tree
Hide file tree
Showing 16 changed files with 4,156 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports = {
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint'],
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
],
parserOptions: {
ecmaVersion: 2020,
sourceType: 'module',
},
rules: {
// Add your custom rules here
},
};
34 changes: 34 additions & 0 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
# For more information see: https://docs.github.com/en/actions/publishing-packages/publishing-nodejs-packages

name: Node.js Package

on:
release:
types: [created]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 16
- run: yarn install --frozen-lockfile
- run: yarn test

publish-npm:
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 16
registry-url: https://registry.npmjs.org/
- run: yarn install --frozen-lockfile
- run: yarn build
- run: yarn publish --access public
env:
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
dist/
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Haqq Network

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
73 changes: 73 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Format Number with Subscript Zeros

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.

[Documentation](https://github.com/haqq-network/format-number-with-subscript-zeros/blob/master/docs/globals.md)

## Installation

```bash
yarn add @haqq/format-number-with-subscript-zeros
```

## Usage

```ts
import { formatNumberWithSubscriptZeros } from '@haqq/format-number-with-subscript-zeros';

console.log(formatNumberWithSubscriptZeros("0.42")); // "0.42"
console.log(formatNumberWithSubscriptZeros("0.042")); // "0.042"
console.log(formatNumberWithSubscriptZeros("0.0042")); // "0.0042"
console.log(formatNumberWithSubscriptZeros("0.00042")); // "0.00042"
console.log(formatNumberWithSubscriptZeros("0.000042")); // "0.000042"
console.log(formatNumberWithSubscriptZeros("0.0000042")); // "0.0₅42"
console.log(formatNumberWithSubscriptZeros("0.00000042")); // "0.0₆42"
console.log(formatNumberWithSubscriptZeros("0.000000042")); // "0.0₇42"
console.log(formatNumberWithSubscriptZeros("0.0000000042")); // "0.0₈42"
console.log(formatNumberWithSubscriptZeros("0.00000000042")); // "0.0₉42"
console.log(formatNumberWithSubscriptZeros("0.000000000042")); // "0.0₁₀42"
console.log(formatNumberWithSubscriptZeros("0.0000000000042")); // "0.0₁₁42"
console.log(formatNumberWithSubscriptZeros("0.00000000000042")); // "0.0₁₂42"
```

### Additional Parameters

The formatNumberWithSubscriptZeros function accepts the following parameters:

- numberStr (string): The number string to format.
- presiction (number, optional): The number of decimal places to include in the formatted string. Default is 3.
- min (number, optional): The minimum value for which the formatting should be applied. Default is 0.00001.

## Running Tests

```sh
yarn test
```

## Building the Project

```sh
yarn build
```

## Linting the Code

```sh
yarn lint
```

## Benefits and Convenience

This method of formatting numbers is particularly useful for:

• Readability: It makes very small numbers easier to read and understand by representing leading zeros as subscript digits.
• Compact Representation: It reduces the length of the number string by using subscript digits for leading zeros.
• Scientific Applications: It is useful in scientific and engineering contexts where numbers can be extremely small and a clear representation is needed.

## How It Works

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.

## License

MIT
77 changes: 77 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
**@haqq/format-number-with-subscript-zeros v1.0.0**[**Docs**](globals.md)

***

# Format Number with Subscript Zeros

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.

[Documentation](https://github.com/haqq-network/format-number-with-subscript-zeros/blob/master/docs/globals.md)

## Installation

```bash
yarn add @haqq/format-number-with-subscript-zeros
```

## Usage

```ts
import { formatNumberWithSubscriptZeros } from '@haqq/format-number-with-subscript-zeros';

console.log(formatNumberWithSubscriptZeros("0.42")); // "0.42"
console.log(formatNumberWithSubscriptZeros("0.042")); // "0.042"
console.log(formatNumberWithSubscriptZeros("0.0042")); // "0.0042"
console.log(formatNumberWithSubscriptZeros("0.00042")); // "0.00042"
console.log(formatNumberWithSubscriptZeros("0.000042")); // "0.000042"
console.log(formatNumberWithSubscriptZeros("0.0000042")); // "0.0₅42"
console.log(formatNumberWithSubscriptZeros("0.00000042")); // "0.0₆42"
console.log(formatNumberWithSubscriptZeros("0.000000042")); // "0.0₇42"
console.log(formatNumberWithSubscriptZeros("0.0000000042")); // "0.0₈42"
console.log(formatNumberWithSubscriptZeros("0.00000000042")); // "0.0₉42"
console.log(formatNumberWithSubscriptZeros("0.000000000042")); // "0.0₁₀42"
console.log(formatNumberWithSubscriptZeros("0.0000000000042")); // "0.0₁₁42"
console.log(formatNumberWithSubscriptZeros("0.00000000000042")); // "0.0₁₂42"
```

### Additional Parameters

The formatNumberWithSubscriptZeros function accepts the following parameters:

- numberStr (string): The number string to format.
- presiction (number, optional): The number of decimal places to include in the formatted string. Default is 3.
- min (number, optional): The minimum value for which the formatting should be applied. Default is 0.00001.

## Running Tests

```sh
yarn test
```

## Building the Project

```sh
yarn build
```

## Linting the Code

```sh
yarn lint
```

## Benefits and Convenience

This method of formatting numbers is particularly useful for:

• Readability: It makes very small numbers easier to read and understand by representing leading zeros as subscript digits.
• Compact Representation: It reduces the length of the number string by using subscript digits for leading zeros.
• Scientific Applications: It is useful in scientific and engineering contexts where numbers can be extremely small and a clear representation is needed.

## How It Works

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.

## License

MIT
35 changes: 35 additions & 0 deletions docs/functions/formatNumberWithSubscriptZeros.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
[**@haqq/format-number-with-subscript-zeros v1.0.0**](../README.md)**Docs**

***

[@haqq/format-number-with-subscript-zeros v1.0.0](../globals.md) / formatNumberWithSubscriptZeros

# Function: formatNumberWithSubscriptZeros()

> **formatNumberWithSubscriptZeros**(`numberStr`, `presiction`, `min`): `string`
Formats a number string using scientific notation with subscript zeros.

## Parameters

**numberStr**: `string`

The number string to format.

**presiction**: `number`= `3`

The number of decimal places to include in the formatted string. Default is 3.

**min**: `number`= `0.00001`

The minimum value for which the formatting should be applied. Default is 0.00001.

## Returns

`string`

The formatted number string.

## Source

[formatNumberWithSubscriptZeros.ts:9](https://github.com/haqq-network/format-number-with-subscript-zeros/blob/df8f6b1745567221a9d0f075968fa0e77abc1479/src/formatNumberWithSubscriptZeros.ts#L9)
9 changes: 9 additions & 0 deletions docs/globals.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[**@haqq/format-number-with-subscript-zeros v1.0.0**](README.md)**Docs**

***

# @haqq/format-number-with-subscript-zeros v1.0.0

## Functions

- [formatNumberWithSubscriptZeros](functions/formatNumberWithSubscriptZeros.md)
5 changes: 5 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
testMatch: ['**/tests/**/*.test.ts'],
};
47 changes: 47 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"name": "@haqq/format-number-with-subscript-zeros",
"version": "1.0.0",
"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.",
"main": "dist/index.js",
"scripts": {
"build": "webpack",
"test": "jest",
"lint": "eslint --ext .ts src/",
"prepublishOnly": "yarn build && yarn test && yarn lint",
"typedoc": "typedoc"
},
"repository": {
"type": "git",
"url": "git+https://github.com/haqq-network/format-number-with-subscript-zeros.git"
},
"keywords": [
"format",
"number",
"subscript",
"zeros",
"scientific",
"notation"
],
"author": "iGroza",
"license": "MIT",
"bugs": {
"url": "https://github.com/haqq-network/format-number-with-subscript-zeros/issues"
},
"homepage": "https://github.com/haqq-network/format-number-with-subscript-zeros#readme",
"devDependencies": {
"@types/jest": "^27.0.1",
"@types/node": "^16.0.0",
"@typescript-eslint/eslint-plugin": "^4.0.0",
"@typescript-eslint/parser": "^4.0.0",
"eslint": "^7.0.0",
"jest": "^27.0.0",
"terser-webpack-plugin": "^5.0.0",
"ts-jest": "^27.0.0",
"ts-loader": "^9.5.1",
"typedoc": "^0.25.13",
"typedoc-plugin-markdown": "^4.0.2",
"typescript": "4.4.4",
"webpack": "^5.0.0",
"webpack-cli": "^4.0.0"
}
}
29 changes: 29 additions & 0 deletions src/formatNumberWithSubscriptZeros.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Formats a number string using scientific notation with subscript zeros.
*
* @param numberStr - The number string to format.
* @param presiction - The number of decimal places to include in the formatted string. Default is 3.
* @param min - The minimum value for which the formatting should be applied. Default is 0.00001.
* @returns The formatted number string.
*/
export function formatNumberWithSubscriptZeros(numberStr: string, presiction = 3, min = 0.00001): string {
const number = parseFloat(numberStr);
if (number >= min) {
const [part0, part1] = numberStr.split('.')
if(part1) {
const leadingZeros = part1?.match?.(/0+/)?.[0] || '';
return `${part0}.${leadingZeros}${part1.replace(leadingZeros, '').slice(0, presiction)}`
}
return part1 ? [part0, part1.slice(0, presiction)].join('.') : part0;
}

const leadingZerosMatch = numberStr.match(/^0\.(0+)/);
if (!leadingZerosMatch) return numberStr;

const leadingZerosCount = leadingZerosMatch[1].length;
const remainingDigits = numberStr.slice(leadingZerosMatch[0].length);

const smallCount = String(leadingZerosCount).split('').map(digit => String.fromCharCode(8320 + parseInt(digit))).join('');

return `0.0${smallCount}${remainingDigits.slice(0, presiction)}`;
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './formatNumberWithSubscriptZeros';
Loading

0 comments on commit 62df504

Please sign in to comment.