{"id":19570214,"url":"https://github.com/zeh/moremath","last_synced_at":"2026-02-24T01:03:00.026Z","repository":{"id":46934060,"uuid":"74290611","full_name":"zeh/moremath","owner":"zeh","description":"The missing JavaScript Math functions","archived":false,"fork":false,"pushed_at":"2024-09-24T06:25:39.000Z","size":429,"stargazers_count":1,"open_issues_count":2,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-10-04T14:17:09.983Z","etag":null,"topics":["clamps","map","math","math-functions","npm-module","range","typescript"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zeh.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-11-20T17:24:05.000Z","updated_at":"2024-01-31T03:06:13.000Z","dependencies_parsed_at":"2023-02-02T21:15:37.252Z","dependency_job_id":null,"html_url":"https://github.com/zeh/moremath","commit_stats":null,"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zeh%2Fmoremath","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zeh%2Fmoremath/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zeh%2Fmoremath/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zeh%2Fmoremath/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zeh","download_url":"https://codeload.github.com/zeh/moremath/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224056540,"owners_count":17248329,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["clamps","map","math","math-functions","npm-module","range","typescript"],"created_at":"2024-11-11T06:14:23.054Z","updated_at":"2025-10-25T23:35:04.816Z","avatar_url":"https://github.com/zeh.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# MoreMath\n\n[![npm](https://img.shields.io/npm/v/moremath.svg)](https://www.npmjs.com/package/moremath)\n[![Build Status](https://travis-ci.org/zeh/moremath.svg?branch=master)](https://travis-ci.org/zeh/moremath)\n[![Coverage Status](https://coveralls.io/repos/github/zeh/moremath/badge.svg?branch=master)](https://coveralls.io/github/zeh/moremath?branch=master)\n[![Dependency Status](https://david-dm.org/zeh/moremath.svg)](https://david-dm.org/zeh/moremath)\n\n*MoreMath* is a library that provides useful Math functions -- functions that either should have been part of the core `Math` package from the start, or that are otherwise commonly needed.\n\n## Install\n\n```shell\nnpm install moremath\n```\n\nMoreMath is written in TypeScript. It can be used both in JavaScript and TypeScript, but in TypeScript projects you get the benefit of code completion by default.\n\n## Usage\n\nMoreMath can be used as a separate package (in which it acts like a *Static Class*) or by importing the specific functions one need.\n\nImport the whole package:\n\n```javascript\n// Import everything (JavaScript ES5)\nvar MoreMath = require('moremath');\n\n// Import everything (JavaScript ES6 and TypeScript)\nimport MoreMath from 'moremath';\n```\n\nThen use any of its functions:\n\n```javascript\nlet value = MoreMath.clamp(11, 0, 10); // 10\n```\n\nAnother way to use it is just importing the functions you want then using them directly.\n\nImport a function:\n\n```javascript\n// Import clamp() (JavaScript ES5)\nvar clamp = require('moremath').clamp;\n\n// Import clamp() (JavaScript ES6 and TypeScript)\nimport { clamp } from 'moremath';\n```\n\nThen use it:\n\n```javascript\nlet value = clamp(11, 0, 10); // 10\n```\n\nThe advantage of importing only specific functions is that the resulting code is shorter and easier to read, and JavaScript packagers that perform *tree-shaking* can get rid of functions that are not used from inside the MoreMath library. In the above case, for example, `clamp` would be the only exported function included in your final project.\n\n\n## Full reference\n\n\n### `map(value:number, oldMin:number, oldMax:number, newMin:number = 0, newMax:number = 1, shouldClamp:Boolean = false):number`\n\nMaps a value from a range, determined by old minimum and maximum values, to a new range, determined by new minimum and maximum values. These minimum and maximum values are referential; the new value is not clamped by them.\n\nParameters:\n\n* `value`: The value to be re-mapped.\n* `oldMin`: The previous minimum value.\n* `oldMax`: The previous maximum value.\n* `newMin`: The new minimum value.\n* `newMax`: The new maximum value.\n\nReturns:\n\n* The new value, mapped to the new range.\n\n\n### `clamp(value:number, min:number = 0, max:number = 1):number`\n\nClamps a number to a range, by restricting it to a minimum and maximum values: if the passed value is lower than the minimum value, it's replaced by the minimum; if it's higher than the maximum value, it's replaced by the maximum; if neither, it's unchanged.\n\nParameters:\n\n* `value`: The value to be clamped.\n* `min`: Minimum value allowed.\n* `max`: Maximum value allowed.\n\nReturns:\n\n* The newly clamped value.\n\n\n### `clampAuto(value:number, clamp1:number = 0, clamp2:number = 1):number`\n\nClamps a number to a range, by restricting it to a range of values: if the passed value is lower than the minimum value, it's replaced by the minimum; if it's higher than the maximum value, it's replaced by the maximum; if neither, it's unchanged.\n\nThis function is similar to `clamp()`, but it switches the range values if necessary, without assuming the first value is the  lower value.\n\nParameters:\n\n* `value`: The value to be clamped.\n* `clamp1`: One end of the allowed range.\n* `clamp2`: Other end of the allowed range.\n\nReturns:\n\n* The newly clamped value.\n\n\n### `getHighestPowerOfTwo(value:number):number`\n\nReturns a power of two value that is higher than the passed value.\n\nParameters:\n\n* `value`: The minimum value desired.\n\nReturns:\n\n* A power of two value that is equal to or higher than the input value.\n\n\n### `getUniqueNumber():number`\n\nReturns a unique number for this session. This is simply a global integer sequence, starting at 1.\n\nReturns:\n\n* A unique integer for the session.\n\n\n### `isPowerOfTwo(value:number):Boolean`\n\nReturns whether a number is a power of two (2, 4, 8, 16, etc).\n\nParameters:\n\n* `value`: A number to be tested.\n\nReturns:\n\n* Whether the input number is a power of two.\n\n\n### `rangeMod(value:number, min:number, pseudoMax:number):number`\n\nRestricts a value to a range, by restricting it to a minimum and maximum values but folding the value to the range instead of simply clamping to the minimum and maximum. It works like a more powerful Modulo function because it allows arbitrary ranges.\n\nParameters:\n\n* `value`: The value to be clamped.\n* `min`: Minimum value allowed.\n* `max`: Pseudo-maximum value allowed. This value is never reached; the minimum would be used instead.\n\nReturns:\n\n* The new value, mapped to the range.\n\nExamples:\n\n```javascript\nconsole.log(rangeMod(14, 0, 10));\n// Result: 4\nconsole.log(rangeMod(360, 0, 360));\n// Result: 0\nconsole.log(rangeMod(360, -180, 180));\n// Result: 0\nconsole.log(rangeMod(21, 0, 10));\n// Result: 1\nconsole.log(rangeMod(-98, 0, 100));\n// Result: 2\n```\n\n## License\n\n[MIT](LICENSE.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzeh%2Fmoremath","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzeh%2Fmoremath","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzeh%2Fmoremath/lists"}