jonahsnider / convert Goto Github PK
View Code? Open in Web Editor NEWThe smallest & fastest library for really easy, totally type-safe unit conversions in TypeScript & JavaScript.
Home Page: https://convert.js.org
License: MIT License
The smallest & fastest library for really easy, totally type-safe unit conversions in TypeScript & JavaScript.
Home Page: https://convert.js.org
License: MIT License
This is not an issue but a feature request to add units of speed to convert. Data sizes (e.g 1tb to kb) would be another great addition. Thanks for all the good work on this so far.
console.log(convert(36, 'h').to('days').toString());
'1.5'
console.log(convert(36, 'h').to('days'));
1.5
console.log(convert(36, 'h').to('best').toString()); // your example of using 'best'
'1.4999999999999998d'
console.log(convert(36, 'h').to('best'));
{
quantity: 1.4999999999999998,
unit: 'd',
toString: ƒ ()
}
There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.
Error type: Cannot find preset's package (@pizzafox)
please double check square because these are not working correctly
square kilometers
square centimeters
square millimeters
square micrometers
convert(2000, 'ft').to('meters')
returns 609.6 which is correct
but
convert(609.6, 'meter').to('ft')
returns 1999.9999999999998 which should be 2000 ft and is incorrect ?
master
branch failed. 🚨I recommend you give this issue a high priority, so other packages depending on you could benefit from your bug fixes and new features.
You can find below the list of errors reported by semantic-release. Each one of them has to be resolved in order to automatically publish your package. I’m sure you can resolve this 💪.
Errors are usually caused by a misconfiguration or an authentication problem. With each error reported below you will find explanation and guidance to help you to resolve it.
Once all the errors are resolved, semantic-release will release your package the next time you push a commit to the master
branch. You can also manually restart the failed CI job that runs semantic-release.
If you are not sure how to resolve this, here is some links that can help you:
If those don’t help, or if this issue is reporting something you think isn’t right, you can always ask the humans behind semantic-release.
The npm token configured in the NPM_TOKEN
environment variable must be a valid token allowing to publish to the registry https://registry.npmjs.org/
.
If you are using Two-Factor Authentication, make configure the auth-only
level is supported. semantic-release cannot publish with the default auth-and-writes
level.
Please make sure to set the NPM_TOKEN
environment variable in your CI with the exact value of the npm token.
Good luck with your project ✨
Your semantic-release bot 📦🚀
I have tried to install and trying to import but it is giving me error like ' Could not find module convert
imported from'. Though I am able to see this in node_modules. Please suggest me how to procceed.
Version used: 5.0.0
Hi, there seems to be a floating point problem when converting from 1 foot into inches.
convert(1, 'ft').to('in') // Results in 12.000000000000002
I know there can be floating point problems in javascript, but I would expect this basic conversion to work properly.
Thanks!
Raymond
Looking through the docs I dont see any conversion types for speed. I often work in inches per second, but threejs uses meters so I have to convert. Would be great to get this built in or the ability to define custom units. If possible, I think a generic solution for this would be best where you can use any already defined length unit over any defined time unit instead of adding in specifically in/s, m/s, miles/h, etc...
With this, the dividend I guess could also be an angle... degrees/s, radians/m, etc.
I'm using two different conversion utilities. meters to feet conversion isn't matching. I realized the ratio used here is rounded and not right.
{ names: ['foot', 'feet'], symbols: ['ft', "'"], ratio: 0.3048 },
1 m = 3.28084 ft
or
1 ft = 0.3047999902464003 m
Issue: Duplicated code and Failure to expand tonnes in mass.ts
Description:
There is a duplication of code in the mass.ts file, specifically in the conversions
array. The expandMacro
method is called twice with the same parameters, resulting in the duplication of conversions and a potential oversight. Additionally, this duplication prevents the proper expansion of units for ton (tonne) measurements.
Steps to Reproduce:
mass.ts
file.conversions
array.expandMacro
method is called twice with the same parameters.Expected Behavior:
The expandMacro
method should be called only once with the appropriate parameters to avoid duplicated conversions. Furthermore, the expansion of units for ton (tonne) measurements should be included.
Actual Behavior:
The expandMacro
method is called twice with the same parameters, leading to duplicate conversions. As a result, the expansion of units for ton (tonne) measurements is not present in the conversions.
Code Snippet:
const poundInGrams = new BigNumber(4.535_923_7e2);
export const mass: ReadonlyDeep<Family> = {
// ...
conversions: [
{names: ['gram', 'grams'], symbols: ['g'], ratio: 1},
...expandMacro(Macros.si, {names: ['gram', 'grams'], symbols: ['g'], ratio: 1}),
{names: ['tonne', 'tonnes', 'metric ton', 'metric tons'], symbols: ['t'], ratio: 1e6},
...expandMacro(Macros.si, {names: ['gram', 'grams'], symbols: ['g'], ratio: 1}),
// ... other conversions ...
],
};
Proposed Solution:
Remove the duplicated expandMacro
method call at line 20, as it duplicates the conversions already added at line 17. Additionally, modify the second expandMacro call to properly expand the units for ton (tonne) measurements.
Replace method call at line 20
...expandMacro(Macros.si, {names: ['gram', 'grams'], symbols: ['g'], ratio: 1}),
with
...expandMacro(Macros.si, {names: ['tonne', 'tonnes', 'metric ton', 'metric tons'], symbols: ['t'], ratio: 1e6}),
First off want to say love this package. Very clean and easy to use.
Feature request:
Thoughts on adding pixels and points to the length type?
It seems that there are no energy units included (joule, kcal, watt, kWhm btu, etc.), I think it would be a great addition.
Affecting version: v4.6.1
TypeError: (0 , convert_1.default) is not a function
2 | describe('test convert package', () => {
3 | it('Project example', () => {
> 4 | const result = convert(1, 'miles').to('km');
| ^
5 | expect(result).toEqual(1.609344);
6 | });
7 | });
Reproduce repo:
https://github.com/ls-jingbo-jin/convert
is it possible to add converstion measure for light year to measure? e.g.
convert(1, "light year").to("kilometer")
master
branch failed. 🚨I recommend you give this issue a high priority, so other packages depending on you can benefit from your bug fixes and new features again.
You can find below the list of errors reported by semantic-release. Each one of them has to be resolved in order to automatically publish your package. I’m sure you can fix this 💪.
Errors are usually caused by a misconfiguration or an authentication problem. With each error reported below you will find explanation and guidance to help you to resolve it.
Once all the errors are resolved, semantic-release will release your package the next time you push a commit to the master
branch. You can also manually restart the failed CI job that runs semantic-release.
If you are not sure how to resolve this, here are some links that can help you:
If those don’t help, or if this issue is reporting something you think isn’t right, you can always ask the humans behind semantic-release.
semantic-release cannot push the version tag to the branch master
on the remote Git repository with URL http://x-access-token:[secure]@github.com/jonahsnider/convert.git
.
This can be caused by:
Good luck with your project ✨
Your semantic-release bot 📦🚀
to continue, in this case if we convert 3 Grams to Petagrams we get 3.0000000000000002e-15 and rounding this number leads to errors
This issue still persists as of v4.13.2. It shows "Uncaught TypeError: convert is not a function" error
Originally posted by @mahony0 in #560 (comment)
Moving discussion here, as the bug you are describing is not the one in the issue originally commented in
master
branch failed. 🚨I recommend you give this issue a high priority, so other packages depending on you can benefit from your bug fixes and new features again.
You can find below the list of errors reported by semantic-release. Each one of them has to be resolved in order to automatically publish your package. I’m sure you can fix this 💪.
Errors are usually caused by a misconfiguration or an authentication problem. With each error reported below you will find explanation and guidance to help you to resolve it.
Once all the errors are resolved, semantic-release will release your package the next time you push a commit to the master
branch. You can also manually restart the failed CI job that runs semantic-release.
If you are not sure how to resolve this, here are some links that can help you:
If those don’t help, or if this issue is reporting something you think isn’t right, you can always ask the humans behind semantic-release.
The npm token configured in the NPM_TOKEN
environment variable must be a valid token allowing to publish to the registry https://registry.npmjs.org/
.
If you are using Two Factor Authentication for your account, set its level to "Authorization only" in your account settings. semantic-release cannot publish with the default "
Authorization and writes" level.
Please make sure to set the NPM_TOKEN
environment variable in your CI with the exact value of the npm token.
Good luck with your project ✨
Your semantic-release bot 📦🚀
Hiya,
I'm trying to convert from squared millimetres (mm2) to squared meters (m2) or 'best' since the values can get quite large.
I'm not finding any options for squared values with intellisense in vscode, but i do see some cubic (m3) conversions.
Am i blind or can someone point me in the right direction?
Heyyya, thanks for creating this library!
It looks like it repeats the well-known Javascript precision error with 0.1 + 0.2 !== 0.3
.
Repro:
https://codepen.io/goodwin64/pen/vYPrMRW?editors=1010
Looking through the pressure conversions available I do not see one for psi, would it be possible to add it?
{names: ['psi'], symbols:['psi'], ratio: new BigNumber(6_894_75729).div(1e5)},
Would be all that was required, no?
master
branch failed. 🚨I recommend you give this issue a high priority, so other packages depending on you can benefit from your bug fixes and new features again.
You can find below the list of errors reported by semantic-release. Each one of them has to be resolved in order to automatically publish your package. I’m sure you can fix this 💪.
Errors are usually caused by a misconfiguration or an authentication problem. With each error reported below you will find explanation and guidance to help you to resolve it.
Once all the errors are resolved, semantic-release will release your package the next time you push a commit to the master
branch. You can also manually restart the failed CI job that runs semantic-release.
If you are not sure how to resolve this, here are some links that can help you:
If those don’t help, or if this issue is reporting something you think isn’t right, you can always ask the humans behind semantic-release.
semantic-release cannot push the version tag to the branch master
on the remote Git repository with URL http://x-access-token:[secure]@github.com/jonahsnider/convert.git
.
This can be caused by:
Good luck with your project ✨
Your semantic-release bot 📦🚀
Would it be possible to make the function return null
or false
when a conversion fails?
For example calling:
convert(360, 'sec').to('min')
will output the whole library code.
Would be nice in it's place to get a falsy value to evaluate against.
All data
conversions fail if using bigint
due to Conversion for [UNIT] to [UNIT] cannot be calculated as a BigInt because the conversion ratio is not an integer
error.
Even a simple operation like this fails:
convert(1024n, 'B').to('KiB');
When doing conversions with US dry quart and US dry pint to litres the values are incorrect (i.e. convert(1, 'US dry quart').to('l')
)
The correct ratio values for dry measures are listed here (https://en.wikipedia.org/wiki/United_States_customary_units#Dry_volume and https://en.wikipedia.org/wiki/Dry_measure#US_units_of_dry_measure) as mentioned in the comments in src/dev/conversions/volume.ts
.
Expected behaviour:
1 US dry quart = 1.101221 L
1 US dry pint = 0.550610 L
Actual behaviour:
1 US dry quart = 0.946352946 L
1 US dry pint = 33600.3125 L
Hi. I found your package today and it's excellent. Thanks so much for putting it together.
One thing I'm always having to do is rounding for display purposes. I'm curious if you'd be interested in adding round
and/or toFixed
with decimal places to your package.
My thinking is that everyone doing these conversions needs to also round for display purposes.
About 6 months ago I tested many solutions and settled on this for speed and accuracy.
// Modified version of Solution #2 from https://stackoverflow.com/a/48764436/25197.
// Fast and accurate.
const DecimalMath = (() => {
const decimalAdjust = (type, num, decimalPlaces) => {
const precision = 10 ** decimalPlaces;
const n = num * precision * (1 + Number.EPSILON);
return Math[type](n) / precision;
};
return {
round(num: number, decimalPlaces = 0) {
return decimalAdjust('round', num, decimalPlaces);
},
// Format using fixed-point notation.
toFixed(num: number, decimalPlaces = 0) {
return decimalAdjust('round', num, decimalPlaces).toFixed(decimalPlaces);
},
ceil(num: number, decimalPlaces = 0) {
return decimalAdjust('ceil', num, decimalPlaces);
},
floor(num: number, decimalPlaces = 0) {
return decimalAdjust('floor', num, decimalPlaces);
},
trunc(num: number, decimalPlaces = 0) {
return decimalAdjust('trunc', num, decimalPlaces);
},
};
})();
export default DecimalMath;
To see the round and toFixed functions in action click the Run Code Snippet
button on this stackoverflow answer. There are 30 answers on that page and I went through the comments in each one to find the places where each answer broke and made sure my version worked correctly.
I imagine a version of convert
that can do this.
convert(150).from('pound').to('kilogram').round(2)
If any of this seems reasonable and you want me to take a stab at a PR I can do that as well.
Feature request: Ability to define custom units
If this is already possible, some documentation on how to do so would be super.
master
branch failed. 🚨I recommend you give this issue a high priority, so other packages depending on you could benefit from your bug fixes and new features.
You can find below the list of errors reported by semantic-release. Each one of them has to be resolved in order to automatically publish your package. I’m sure you can resolve this 💪.
Errors are usually caused by a misconfiguration or an authentication problem. With each error reported below you will find explanation and guidance to help you to resolve it.
Once all the errors are resolved, semantic-release will release your package the next time you push a commit to the master
branch. You can also manually restart the failed CI job that runs semantic-release.
If you are not sure how to resolve this, here is some links that can help you:
If those don’t help, or if this issue is reporting something you think isn’t right, you can always ask the humans behind semantic-release.
An npm token must be created and set in the NPM_TOKEN
environment variable on your CI environment.
Please make sure to create an npm token and to set it in the NPM_TOKEN
environment variable on your CI environment. The token must allow to publish to the registry https://registry.npmjs.org/
.
Good luck with your project ✨
Your semantic-release bot 📦🚀
Hi, I couldn't find any information about doing a round operation automatically or even triggering it with some param.
So while the examples in readme seems to be accurate, the example of convert(1930536, 'meters').to('best').toString()
returns me an ugly result of 1930.5359999999998km
. I expected to maybe get 1930.54km
as a result when using a "best" param.
Converting from F to C results in incorrect value.
const tempC = convert(68.0, "fahrenheit").to("celsius");
the output is: 5.777777777777779
while it should be 20
Converting from C to F works as expected and K to C does, too.
When using the "best"
parameter for data conversion it always returns the binary unit (KiB, MiB, etc.) for both the "imperial"
or "metric"
parameter. I think when using the "metric" param, it should be returning MB, GB, etc.
This seems to be a simple change in your bestUnits
type for the "metric"
kind, was hoping this could be updated.
Thanks!
convert(1000).from('ml').to('l') , I am expecting output as 1.
Is there any support for BTU? sorry if this is the wrong channel.
International feet (ft) is supported but it would be useful to have U.S feet too
This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.
These updates await pending status checks before automerging. Click on a checkbox to abort the branch automerge, and create a PR instead.
@vitest/coverage-v8
, @vitest/ui
, vitest
)These updates have all been created already. Click a checkbox below to force a retry/rebase of any.
.github/workflows/ci.yml
actions/checkout v4
actions/setup-node v4
oven-sh/setup-bun v1
actions/checkout v4
actions/setup-node v4
oven-sh/setup-bun v1
.github/workflows/release-please.yml
google-github-actions/release-please-action v4
actions/checkout v4
actions/setup-node v4
oven-sh/setup-bun v1
actions/setup-node v4
.node-version
node 20
package.json
@babel/core 7.24.6
@babel/preset-env 7.24.6
@biomejs/biome 1.7.3
@jonahsnider/util 11.0.0
@microsoft/api-extractor 7.43.8
@rollup/plugin-babel 6.0.4
@rollup/plugin-node-resolve 15.2.3
@rollup/plugin-swc 0.3.0
@rollup/plugin-terser 0.4.4
@size-limit/preset-small-lib 11.1.4
@swc/core 1.5.7
@tsconfig/node20 20.1.4
@tsconfig/strictest 2.0.5
@types/node 20.12.12
@vitest/coverage-v8 1.3.1
@vitest/ui ^1.2.2
bignumber.js 9.1.2
just-install 2.0.1
rollup 4.18.0
size-limit 11.1.4
typedoc 0.25.13
typescript 5.4.5
vitest 1.3.1
yarn 4.2.2
I tried the unpkg link for this repo and it did not work. I hacked the script to extract the convert, convertMany, and ms functions out (if you're looking to do this just set e
to an external variable where e.convert is being set.)
Maybe I did something wrong but I had to hack it to extract the convert function at all. It doesn't set on the window.
Add Kb
and KB
to data digital units.
Is there a way to use m
for minutes with the ms shorthand utility? I see m
is used for meters currently but its also commonly used for minutes. (even in the ms package)
Hello, a problem with rounding by all measures was found.
for example, if we take 10 square meters and translate them into square kilometers, we get 0.000009999999999999999, while if we take 11 we get the correct answer 0.000011, and so on for all metrics
Thanks for your consideration...
Currently
convert(16, 'tablespoon').to('best')
results in 236.5882365 mL, which is accurate but perhaps not the best option as 16 tablespoons = 1 cup.
It would great if the best option could be restricted to the measurement system of the input.
Or perhaps I have missed how to do that today?
(convert(1400, "kg").to("t")
出现1.4000000000000001
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.