{"id":15681784,"url":"https://github.com/transitive-bullshit/id-shortener","last_synced_at":"2025-05-07T12:44:20.105Z","repository":{"id":57271067,"uuid":"93164432","full_name":"transitive-bullshit/id-shortener","owner":"transitive-bullshit","description":"Efficient id / url shortener for NodeJS backed by pluggable storage defaulting to redis.","archived":false,"fork":false,"pushed_at":"2020-07-11T23:14:15.000Z","size":42,"stargazers_count":9,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-10T22:17:47.194Z","etag":null,"topics":["id-shortener","objectid","redis","shortener","url","url-shortener"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/transitive-bullshit.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-06-02T12:43:11.000Z","updated_at":"2025-02-28T03:02:20.000Z","dependencies_parsed_at":"2022-09-06T21:50:43.893Z","dependency_job_id":null,"html_url":"https://github.com/transitive-bullshit/id-shortener","commit_stats":null,"previous_names":["fisch0920/id-shortener"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/transitive-bullshit%2Fid-shortener","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/transitive-bullshit%2Fid-shortener/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/transitive-bullshit%2Fid-shortener/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/transitive-bullshit%2Fid-shortener/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/transitive-bullshit","download_url":"https://codeload.github.com/transitive-bullshit/id-shortener/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252881102,"owners_count":21819102,"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":["id-shortener","objectid","redis","shortener","url","url-shortener"],"created_at":"2024-10-03T16:59:50.888Z","updated_at":"2025-05-07T12:44:20.083Z","avatar_url":"https://github.com/transitive-bullshit.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# id-shortener\n\n\u003e Efficient id / url shortener backed by pluggable storage defaulting to redis.\n\n[![NPM](https://img.shields.io/npm/v/id-shortener.svg)](https://www.npmjs.com/package/id-shortener) [![Build Status](https://img.shields.io/circleci/project/github/transitive-bullshit/id-shortener.svg)](https://circleci.com/gh/transitive-bullshit/id-shortener) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)\n\n## Features\n\n- Efficient, minimal short ID generation and long ID retrieval\n- Pluggable storage backend defaulting to redis\n  - Supports [node_redis](https://github.com/NodeRedis/node_redis) and [ioredis](https://github.com/luin/ioredis) interchangeably\n- Customizable short ID character set\n  - Defaults to [base 60](http://ttk.me/w/NewBase60) to maximize URL compatibility\n- Customizable idempotency\n  - Eg, if you shorten the same ID multiple times, you may always return the same short ID (default `idempotent: true`) or always returning unique IDs (`idempotent: false`)\n- Thorough unit tests running against an actual instance of redis via [docker-compose](https://docs.docker.com/compose/)\n\n## Install\n\n```bash\nnpm install --save id-shortener\n```\n\n## Usage\n\n```js\nconst IdShortener = require('id-shortener')\n\n// create a redis client as the backend\nconst Redis = require('ioredis')\nconst redisClient = new Redis('redis://localhost:6379')\n\nconst shortener = new IdShortener({\n  client: redisClient\n})\n\nconst shortId = await shortener.shorten('test-key') =\u003e very short base60 string\nconst longId = await shortener.expand(shortId) // =\u003e 'test-key'\n```\n\n## API\n\n### class IdShortener(opts)\n\n- `opts.client` - object, *required* client to use for storage backend\n  - currently supports instances of [node_redis](https://github.com/NodeRedis/node_redis) and [ioredis](https://github.com/luin/ioredis)\n- `opts.characters` - string, optional character set to use for short IDs (default [base 60](http://ttk.me/w/NewBase60))\n- `opts.idempotent` - boolean, optional whether or not `shorten` should be idempotent (default `true`)\n\n_redis-specific_\n- `opts.shortToLongKey` - string, optional key to use for short to long mapping (default `'id-shortener:short-to-long'`)\n- `opts.longToShortKey` - string, optional key to use for long to short mapping (default `'id-shortener:long-to-short'`)\n  - _Note_ this key is only used if `idempotent` is `true`\n- `opts.sequenceKey` - string, optional key to use for atomic id counter (default `'id-shortener:sequence'`)\n\n#### shorten\n\nReturns a shortened version of the given long ID. _Note_ this method's semantics are highly affected by the `idempotent` option that was passed to the `IdShortener` constructor.\n\n`IdShortener.shorten(string longId) =\u003e Promise\u003cstring shortId\u003e`\n\n#### expand\n\nReturns the long version of the given short ID. _Note_ that this method will return `undefined` if the `shortId` is not found.\n\n`IdShortener.expand(string shortId) =\u003e Promise\u003cstring longId\u003e`\n\n## License\n\nMIT © [Travis Fischer](https://github.com/transitive-bullshit)\n\n## Why??? 💩\n\nBuilding a URL shortener is a [very](https://stackoverflow.com/questions/742013/how-to-code-a-url-shortener), [very](http://n00tc0d3r.blogspot.com) [common](https://www.hiredintech.com/classrooms/system-design/lesson/55) [interview](https://www.interviewbit.com/problems/tiny-url/) [question](https://www.educative.io/collection/page/5668639101419520/5649050225344512/5668600916475904) with lots of [great](https://github.com/Hedronium/url-shorty) [existing](https://gist.github.com/bhelx/778542) [example](https://github.com/delight-im/ShortURL) [solutions](https://gist.github.com/epeli/1158171).\n\nThat being said, I couldn't find an up-to-date, open source, NodeJS module that fit my project's needs.\n\nWith this in mind, the goal of this module is not to be a perfect URL shortening solution ([xkcd](https://xkcd.com/927/)), as every use case will inevitably have unique differences, but rather to provide a solid example implementation for other Node devs. ✌️\n\n![](http://i.giphy.com/3o6Zt4rDm1bx6iLHYA.gif)\n\nSupport my OSS work by \u003ca href=\"https://twitter.com/transitive_bs\"\u003efollowing me on twitter \u003cimg src=\"https://storage.googleapis.com/saasify-assets/twitter-logo.svg\" alt=\"twitter\" height=\"24px\" align=\"center\"\u003e\u003c/a\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftransitive-bullshit%2Fid-shortener","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftransitive-bullshit%2Fid-shortener","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftransitive-bullshit%2Fid-shortener/lists"}