npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@dunkelhaiser/numeri-romani

v1.0.3

Published

Library for work with Roman numbers.

Downloads

256

Readme

Numeri Romani

JavaScript library for work with Roman numbers.

Features

  • TypeScript and JSDoc support
  • Conversion between Roman and Arabic numbers
  • RomanNumber class
  • Ability to perform basic mathematical operations

Installation

npm install @dunkelhaiser/numeri-romani
yarn add @dunkelhaiser/numeri-romani
pnpm add @dunkelhaiser/numeri-romani
bun add @dunkelhaiser/numeri-romani

Basic usage

Importing the Library

Using ESM

import { RomanNumber, romanNumerals, romanize, romanizeSafe, arabicize, arabicizeSafe, isValidRoman, isValidRomanSafe, isValidArabic, isValidArabicSafe } from "@dunkelhaiser/numeri-romani";

Using CommonJS

const { RomanNumber, romanNumerals, romanize, romanizeSafe, arabicize, arabicizeSafe, isValidRoman, isValidRomanSafe, isValidArabic, isValidArabicSafe } = require("@dunkelhaiser/numeri-romani");

Number conversion

Convert to Roman number

romanize(379); // => "CCCLXXIX"

Convert to Arabic number

arabicize("LIV"); // => 54

With the default implementation of these functions, if conversion fails, a corresponding error will be thrown explaining the reason for the failure. Default functions use isValidRoman and isValidArabic, so errors will be the same as theirs. If you don't want to handle errors, you can use safe implementations of these functions romanizeSafe or arabicizeSafe which on fail will return "" or NaN correspondingly.

Validity check

Check the validity of the Roman number

To check if a string is a valid Roman number you can use isValidRoman or isValidRomanSafe.

isValidRoman("LIV"); // => true
isValidRoman("IIV"); // => Error: Invalid roman number
isValidRomanSafe("LIV"); // => true
isValidRomanSafe("IIV"); // => false

Check the validity of the Arabic number

To check if an Arabic number can be converted into a Roman number you can use isValidArabic or isValidArabicSafe.

isValidArabic("LIV"); // => true
isValidArabic(5.5); // => Error: Cannot convert non-integer number
isValidArabic(0); // => Error: Cannot convert zero
isValidArabic(-2); // => Error: Cannot convert negative numbers
isValidArabic(-4567); // => Error: Cannot convert numbers greater than 3999
isValidArabicSafe(14); // => true
isValidArabicSafe(5.5); // => false
isValidArabicSafe("14"); // => false

RomanNumber class

RomanNumber class provides an ability to create RomanNumber an object that will contain Roman and Arabic numeral values. Also, this object provides arithmetic operations methods.

Initialization

const romanNumber = new RomanNumber(7); // { roman: "VII", arabic: 7 }
const romanNumber = new RomanNumber("VII"); // { roman: "VII, arabic: 7 }
Get values
romanNumber.getValue(); // => "VII"
romanNumber.getNumericValue(); // => 7
romanNumber.getValues(); // => { roman: "VII", arabic: 7 }

Set value

romanNumber.setValue(9); // { roman: "IX", arabic: 9 }
romanNumber.setValue("IX"); // { roman: "IX", arabic: 9 }

Arithmetic operations

Arithmetic operations will update Roman and Arabic values of an instantiated object and return it, so it can be reassigned or method chained.

All methods support Arabic and Roman numbers as an argument. The Roman number must correspond to the isValidRoman function. Arabic number can be any integer value.

The results of operations must correspond to the isValidRoman function or an error will be thrown.

Addition
romanNumber.add(4).add("IV");
Subtraction
romanNumber.substract(4).subtract("IV");
Multiplication
romanNumber.multiply(4).multiply("IV");
Division
romanNumber.divide(4).divide("IV");

Contributing

Bug Reporting

If you come across a bug or unexpected behavior, please take the time to report it. To file a bug report:

  1. Check if the issue has already been reported by searching the issues.
  2. If the issue hasn't been reported yet, open a new issue, providing as much detail as possible, including:
  • A clear and concise title.
  • A detailed description of the issue.
  • Steps to reproduce the problem.
  • Expected and actual behavior.

Feature Proposals

To propose a new feature:

  1. Check the issues to ensure it hasn't been proposed before.
  2. Open a new issue, clearly describing the new feature or enhancement you would like to see.
  3. Provide any relevant details or use cases that will help understand the use of the proposed feature.

Code Contributions

To contribute code:

  1. Fork the repository.
  2. Create a new branch for your changes with a specific prefix: git checkout -b feat/your-feature. Accepted prefixes: feat, fix, refactor, docs.
  3. Make your changes, following the coding style.
  4. Write unit tests for your changes.
  5. Update the README or documentation if necessary.
  6. Submit a pull request to the dev branch of the original repository.
  7. Provide a detailed description in the pull request, explaining the purpose of your changes.

License

Copyright (c) 2024 Kyrylo Tymchyshyn
Licensed under the MIT license.

ko-fi