{"id":16125529,"url":"https://github.com/semibran/wrap-around","last_synced_at":"2025-03-18T13:30:45.362Z","repository":{"id":57149231,"uuid":"95520650","full_name":"semibran/wrap-around","owner":"semibran","description":":recycle: Wrap numbers within a certain range","archived":false,"fork":false,"pushed_at":"2017-06-27T06:54:05.000Z","size":4,"stargazers_count":17,"open_issues_count":2,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-28T09:55:37.234Z","etag":null,"topics":["around","range","wrap"],"latest_commit_sha":null,"homepage":"","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/semibran.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"license","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-06-27T05:25:55.000Z","updated_at":"2023-04-04T16:59:11.000Z","dependencies_parsed_at":"2022-08-29T20:51:20.270Z","dependency_job_id":null,"html_url":"https://github.com/semibran/wrap-around","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/semibran%2Fwrap-around","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/semibran%2Fwrap-around/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/semibran%2Fwrap-around/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/semibran%2Fwrap-around/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/semibran","download_url":"https://codeload.github.com/semibran/wrap-around/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243928186,"owners_count":20370247,"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":["around","range","wrap"],"created_at":"2024-10-09T21:29:55.766Z","updated_at":"2025-03-18T13:30:45.066Z","avatar_url":"https://github.com/semibran.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# wrap-around\n![table of values](table.png)\n\nRestricts a number `n` to the interval `0 \u003c= n \u003c m` by \"wrapping it around\" within said range.\n```js\nconst wrap = require('wrap-around')\nvar result = {}\nfor (var m = 3, n = -3; n \u003c 9; n++) {\n  result[n] = wrap(m, n)\n}\n```\n\nSome possible uses of this function include:\n- restricting an index to valid array indices\n```js\nindex = wrap(array.length, index)\n```\n- selecting items from the end of a list (Python-style!)\n```js\n\u003e var array = ['foo', 'bar', 'baz']\n\u003e array[wrap(array.length, -1)]\n'baz'\n```\n- wrapping the player around the screen in a game of Pac-Man, Snake, etc.\n```js\nhero.position = [wrap(width, x), wrap(height, y)]\n```\n\n## usage\n[![NPM](https://nodei.co/npm/wrap-around.png?mini)](https://www.npmjs.com/package/wrap-around)\n\n### Why not just use `n % m`?\nWhile the modulo operator wraps positive values with ease (it's actually used internally by the `wrap` function), it takes a bit more setup to handle negative values correctly. Consider the following example, in which `%` fails to provide the desired result:\n```js\n\u003e -1 % 3\n-1\n\n\u003e wrap(3, -1)\n2\n```\n\n### What about loops?\nUsing loops for this kind of thing is a handy way of demonstrating what exactly this function does - `wrap(m, n)` produces the same result as two loops forcing a number between the desired range.\n```js\nwhile (n \u003c 0) n += m\nwhile (n \u003e= m) n -= m\n```\n\nUnfortunately, they're also 300x slower. :grimacing:\n```md\n# wrap 100000 times\nok ~4.7 ms (0 s + 4696167 ns)\n\n# loop 100000 times\nok ~1.36 s (1 s + 359910028 ns)\n```\n\nSo at the end of the day, you're better off avoid loops for \"wrapping\" numbers in production code. Use the modulo `%` operator for positive numbers or `wrap-around` if you plan on handling negative numbers.\n\n## license\n[MIT](https://opensource.org/licenses/MIT) © [Brandon Semilla](https://git.io/semibran)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsemibran%2Fwrap-around","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsemibran%2Fwrap-around","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsemibran%2Fwrap-around/lists"}