├── .editorconfig ├── .gitattributes ├── .github └── workflows │ └── main.yml ├── .gitignore ├── .npmrc ├── index.js ├── license ├── package.json ├── readme.md └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto 2 | *.js text eol=lf 3 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 14 14 | - 12 15 | - 10 16 | - 8 17 | - 6 18 | - 4 19 | steps: 20 | - uses: actions/checkout@v2 21 | - uses: actions/setup-node@v1 22 | with: 23 | node-version: ${{ matrix.node-version }} 24 | - run: npm install 25 | - run: npm test 26 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | module.exports = function () { 3 | let ret = 0; 4 | const len = arguments.length; 5 | 6 | for (let i = 0; i < len; i++) { 7 | const x = arguments[i]; 8 | 9 | if (x === Infinity || x === -Infinity) { 10 | return Infinity; 11 | } 12 | 13 | ret += x * x; 14 | } 15 | 16 | return Math.sqrt(ret); 17 | }; 18 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (sindresorhus.com) 4 | 5 | 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: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | 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. 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "hypot", 3 | "version": "1.0.1", 4 | "description": "ES2015 Math.hypot() ponyfill", 5 | "license": "MIT", 6 | "repository": "sindresorhus/hypot", 7 | "author": { 8 | "name": "Sindre Sorhus", 9 | "email": "sindresorhus@gmail.com", 10 | "url": "sindresorhus.com" 11 | }, 12 | "engines": { 13 | "node": ">=4" 14 | }, 15 | "scripts": { 16 | "test": "ava" 17 | }, 18 | "files": [ 19 | "index.js" 20 | ], 21 | "keywords": [ 22 | "es2015", 23 | "ponyfill", 24 | "polyfill", 25 | "shim", 26 | "number", 27 | "math", 28 | "hypot", 29 | "square", 30 | "root", 31 | "sum" 32 | ], 33 | "devDependencies": { 34 | "ava": "*", 35 | "number-is-nan": "^1.0.0" 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | Deprecated as this is now natively available in all modern browsers and Node.js versions. 2 | 3 | --- 4 | 5 | # hypot 6 | 7 | > ES2015 [`Math.hypot()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/hypot) [ponyfill](https://ponyfill.com) 8 | 9 | 10 | ## Install 11 | 12 | ``` 13 | $ npm install hypot 14 | ``` 15 | 16 | 17 | ## Usage 18 | 19 | ```js 20 | const hypot = require('hypot'); 21 | 22 | hypot(3, -4); 23 | //=> 5 24 | ``` 25 | 26 | 27 | ## License 28 | 29 | MIT © [Sindre Sorhus](https://sindresorhus.com) 30 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import numberIsNan from 'number-is-nan'; 3 | import m from '.'; 4 | 5 | test(t => { 6 | t.is(m(), 0); 7 | t.is(m(0), 0); 8 | t.is(m(1), 1); 9 | t.is(m(2), 2); 10 | t.is(m(-3), 3); 11 | t.is(m(0, 0, 1), 1); 12 | t.is(m(0, 1, 0), 1); 13 | t.is(m(1, 0, 0), 1); 14 | t.is(m(3, -4), 5); 15 | t.is(m(3, 4), 5); 16 | t.is(m(3, 4, 5), 7.0710678118654755); 17 | t.is(m(0, -0), 0); 18 | t.is(m(0, 0), 0); 19 | t.is(m(0, 1), 1); 20 | t.is(m(0, -1), 1); 21 | t.is(m(-0, -0), 0); 22 | t.is(m(-0, 0), 0); 23 | t.is(m(-0, 1), 1); 24 | t.is(m(-0, -1), 1); 25 | t.is(m(-3, 4), 5); 26 | t.is(m(Infinity, 0), Infinity); 27 | t.is(m(-Infinity, 0), Infinity); 28 | t.is(m(0, Infinity), Infinity); 29 | t.is(m(0, -Infinity), Infinity); 30 | t.is(m(Infinity, NaN), Infinity); 31 | t.is(m(NaN, -Infinity), Infinity); 32 | t.true(numberIsNan(m(NaN))); 33 | t.true(numberIsNan(m(3, 4, 'foo'))); 34 | t.true(numberIsNan(m(NaN, 0))); 35 | t.true(numberIsNan(m(0, NaN))); 36 | }); 37 | --------------------------------------------------------------------------------