Skip to content

Commit

Permalink
Initial commit with clean history
Browse files Browse the repository at this point in the history
  • Loading branch information
tal7aouy committed Sep 17, 2024
0 parents commit 02451e2
Show file tree
Hide file tree
Showing 23 changed files with 14,693 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = false
insert_final_newline = false
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
build
13 changes: 13 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"root": true,
"parser": "@typescript-eslint/parser",
"plugins": [
"@typescript-eslint"
],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
"plugin:prettier/recommended"
]
}
1 change: 1 addition & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ko_fi: tal7aouy
18 changes: 18 additions & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Check

on:
push:
branches:
- master

jobs:
lint-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v3
with:
node-version: '16'
- run: npm install --production=false
- run: npx eslint . --ext .ts
- run: npm test
25 changes: 25 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Publish

on:
push:
tags:
- v*

jobs:
publish:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v1

- uses: actions/setup-node@v3
with:
node-version: 16
registry-url: https://registry.npmjs.org

- run: npm install --production=false

- run: npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
36 changes: 36 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Logs
logs
*.log

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul or jest
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules

# Vim
*.swp

# TypeScript compiled files
build/

# MacOS
.DS_Store
3 changes: 3 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.github
tsconfig.json
tslint.json
5 changes: 5 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
semi: true
trailingComma: 'all'
singleQuote: true
printWidth: 80
tabWidth: 2
41 changes: 41 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Changelog

All notable changes to this project will be documented in this file.

## [1.0.0]

- initial release

## [1.1.0] (Feb-9-2022)

- use aveta in command line
- update Readme

## [1.1.1] (Feb-17-2022)

- update readme
- use aveta with `npx`

## [1.2.0] (Mar-06-2022)

- update readme
- specify a maximum number of significant digits for a number
## [1.3.0] (Mar-24-2023)

- update readme
- specify a maximum number of significant digits for a number


## [1.4.0] (Upcoming)

### Added
- **Custom base scaling**: Introduced the `base` option, allowing users to specify a custom base for scaling numbers (e.g., 1000 or 1024 for file sizes).
- **Rounding mode**: Added `roundingMode` option with values `'up'`, `'down'`, or `'nearest'` to control how numbers are rounded.
- **Threshold for formatting**: Added `threshold` option to set a minimum value for applying unit conversion (below the threshold, the number will be returned as-is).
- **Negative number formatting**: Introduced the `negativeFormat` option to control how negative numbers are displayed (`'prefix'` for minus sign or `'parentheses'` for enclosing in parentheses).
- **Scientific notation**: Added the `scientificNotation` option to toggle the display of large numbers in scientific notation.
- **Localized units**: Introduced the `localizedUnits` option to support locale-specific unit abbreviations (e.g., different units for English and French).
- **Custom number format**: Added the `numberFormat` option, which allows the use of a custom function to format numbers before unit conversion.

### Updated
- **Documentation**: Updated the README to reflect the new options added in this release.
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
The MIT License (MIT)

Copyright (c) 2022 Mhammed Talhaouy

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.

200 changes: 200 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
# Aveta

<h1 align="center">
<br>
<img src="icon.png" alt="logo" width="200">
</h1>

<h4 align="center">Convert large numbers into concise, human-readable formats easily.</h4>

| Input :disappointed: | Output :joy: |
| -------------------- | -------------- |
| `6000` | `'6K'` |
| `10000` | `'10K'` |
| `42500` | `'42.5K'` |
| `1250000` | `'1.25M'` |

## Installation

### With npm:

```bash
npm install aveta
```

### With Yarn:

```bash
yarn add aveta
```

## Usage

```typescript
aveta(value: number, options?: Partial<IOptions>): string;
```

```js
import aveta from 'aveta';

// For CommonJS: `const { aveta } = require('aveta');`

aveta(8700); // '8.7K'

aveta(123456, {
digits: 3,
lowercase: true,
});
// '123k'

aveta(4567, {
digits: 3,
lowercase: true,
});
// '4.57k'

aveta(2048000, {
precision: 2,
lowercase: true,
});
// '2.48m'

aveta(45500, {
precision: 3,
separator: ',',
});
// '45,500K'

aveta(1440000, {
units: ['B', 'KB', 'MB', 'GB', 'TB'],
space: true,
});
// '1.44 MB'
```

### Command Line Usage

You can also use `aveta` directly in the terminal.

```bash
$ aveta 234000
# or
$ npx aveta 234000
234K
```

For more options, run:

```bash
aveta --help
```

## Default Options

| Name | Type | Default | Description |
| ------------------ | -------------------------------- | --------------------------------------- | --------------------------------------------------------------------------- |
| `precision` | `number` | `1` | Number of decimal places to round to |
| `digits` | `number` | `0` | Number of significant digits to display |
| `separator` | `string` | `'.'` | Decimal separator (e.g. `.` or `,`) |
| `lowercase` | `boolean` | `false` | Output unit abbreviations in lowercase |
| `space` | `boolean` | `false` | Insert a space between the number and unit abbreviation |
| `units` | `Array<string>` | `['', 'K', 'M', 'B', 'T', 'P', 'E']` | Units to use for thousand, million, billion, etc. |
| `base` | `number` | `1000` | Base to scale numbers (default is 1000 for K, M, etc.) |
| `roundingMode` | `'up' \| 'down' \| 'nearest'` | `'nearest'` | How numbers are rounded: nearest, always up, or always down |
| `threshold` | `number` | `0` | Minimum value before unit conversion is applied |
| `negativeFormat` | `'prefix' \| 'parentheses'` | `'prefix'` | How negative numbers are displayed (e.g., with `-` or in parentheses) |
| `scientificNotation`| `boolean` | `false` | Use scientific notation for very large/small numbers |
| `localizedUnits` | `{ [locale: string]: string[] }` | `undefined` | Provide localized units for different locales |
| `numberFormat` | `(value: number) => string` | `undefined` | Custom function to format numbers |

---

### New Options

#### **`base`**

Specifies the base used for scaling the number. The default is `1000` (e.g., `K` for thousand), but you can change this to another base like `1024` for file sizes (e.g., `KB`, `MB`).

```js
aveta(1048576, { base: 1024, units: ['B', 'KB', 'MB', 'GB'] });
// '1 MB'
```

#### **`roundingMode`**

Controls how numbers are rounded:

- `'nearest'` (default): rounds to the nearest number.
- `'up'`: always rounds up.
- `'down'`: always rounds down.

```js
aveta(4567, { roundingMode: 'up' });
// '5K'
```

#### **`threshold`**

Sets a threshold value below which numbers will not be formatted with units. For example, if the threshold is `1000`, any number below `1000` will be returned as is.

```js
aveta(999, { threshold: 1000 });
// '999'
```

#### **`negativeFormat`**

Controls how negative numbers are displayed:

- `'prefix'` (default): negative numbers are prefixed with a minus sign (`-`).
- `'parentheses'`: negative numbers are enclosed in parentheses.

```js
aveta(-5000, { negativeFormat: 'parentheses' });
// '(5K)'
```

#### **`scientificNotation`**

If `true`, very large or small numbers will be displayed in scientific notation.

```js
aveta(1e12, { scientificNotation: true });
// '1.00e+12'
```

#### **`localizedUnits`**

Allows the specification of units based on locale. You can pass an object where the keys are locale strings and the values are arrays of unit abbreviations.

```js
aveta(1440000, {
localizedUnits: {
en: ['B', 'KB', 'MB', 'GB', 'TB'],
fr: ['o', 'Ko', 'Mo', 'Go', 'To'],
},
});
// '1.44 MB' (in English)
// '1,44 Mo' (in French)
```

#### **`numberFormat`**

Provides a custom function to format the number before applying unit abbreviations. This is useful for complex formatting needs.

```js
aveta(12345, {
numberFormat: (value) => value.toLocaleString('en-US', { style: 'currency', currency: 'USD' }),
});
// '$12,345'
```

---

## License

Aveta is available under the [MIT License](LICENSE).

## Authors

Created by [Mhammed Talhaouy](https://github.com/tal7aouy).
Loading

0 comments on commit 02451e2

Please sign in to comment.