Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix biginteger errors, update deps, improve build #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ build/Release
# Deployed apps should consider commenting this line out:
# see https://npmjs.org/doc/faq.html#Should-I-check-my-node_modules-folder-into-git
node_modules
package-lock.json
16 changes: 12 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
build:
.PHONY: build unbuild test

build: src/*.coffee
src/*.coffee:
cp -R lib src
./node_modules/.bin/coffee -c lib
find lib -iname "*.coffee" -exec rm '{}' ';'
npx coffee -c lib
rm lib/*.coffee

unbuild:
unbuild: lib/*.coffee
lib/*.coffee:
rm -rf lib
mv src lib

test: src/*.coffee
npx coffee -c test && npx mocha
rm test/*.js
66 changes: 0 additions & 66 deletions lib/charmap.json

This file was deleted.

24 changes: 7 additions & 17 deletions lib/index.coffee
Original file line number Diff line number Diff line change
@@ -1,24 +1,14 @@
BigNumber = require 'bignumber.js'
map = require './charmap'

# `map` is for converting from the number -> character parings that JS uses,
# into the standard base64 table
reversedMap = {}
for i, o of map
reversedMap[o] = i
BigNumber.config({
ALPHABET: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789$_',
});

instagramIdToUrlSegment = (id) ->
id = (new BigNumber(id)).toString(64)
urlSegment = ''
for char in id
urlSegment += map[char]
return urlSegment
if (typeof id == 'number' && id > Number.MAX_SAFE_INTEGER)
throw ('Input value too large, please pass as string')
(new BigNumber(id)).toString(64).replace(/\$/g, '-')

urlSegmentToInstagramId = (urlSegment) ->
id = ''
for char in urlSegment
id += reversedMap[char]
id = (new BigNumber(id, 64)).toString(10)
return id
(new BigNumber(urlSegment.replace(/-/g, '$'), 64)).toString(10)

module.exports = {instagramIdToUrlSegment, urlSegmentToInstagramId}
5 changes: 5 additions & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
declare namespace instagramId {
const instagramIdToUrlSegment: (id: string) => string;
const urlSegmentToInstagramId: (urlSegment: string) => string;
}
export = instagramId
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "instagram-id-to-url-segment",
"description": "Convert Instagram post IDs into Instagram links, algorithmically",
"version": "0.0.0",
"version": "1.0.0",
"author": "Sean Lang <[email protected]>",
"dependencies": {
"bignumber.js": "^2.0.7"
"bignumber.js": "^9.0.1"
},
"devDependencies": {
"coffee-script": "^1.9.3",
"mocha": "^1.21.4",
"coffeescript": "^2.5.1",
"mocha": "^8.4.0",
"should": "^4.0.4"
},
"keywords": [
Expand All @@ -21,6 +21,6 @@
"main": "lib",
"repository": "git://github.com/slang800/instagram-id-to-url-segment.git",
"scripts": {
"test": "mocha"
"test": "make test; make unbuild"
}
}
29 changes: 0 additions & 29 deletions test/index.coffee
Original file line number Diff line number Diff line change
@@ -1,37 +1,8 @@
should = require 'should'
BigNumber = require 'bignumber.js'
{instagramIdToUrlSegment, urlSegmentToInstagramId} = require '../lib'
samples = require './samples'
map = require '../lib/charmap'

describe 'instagram-id-to-url-segment', ->
it 'should have a correct internal charmap', ->
# this is for mapping from the JS number conversion charmap to the base64
# alphabet
CHAR_MAPS = [
[[0, 25], [65, 90]] # (uppercase) 36 - 52 becomes 65 - 90
[[26, 51], [97, 122]] # (lowercase) 10 - 35 becomes 97 - 122
[[52, 61], [48, 57]] # (number) 0 - 9 becomes 48 - 57
[[62, 62], [45, 45]] # (hyphen) 62 becomes 45
[[63, 63], [95, 95]] # (underscore) 63 becomes 95
]

mapChar = (char, reverse = false) ->
[from, to] = (if not reverse then [0, 1] else [1, 0])

for charMap in CHAR_MAPS
[startRange, endRange] = charMap[from]
offset = charMap[to][0] - startRange
if startRange <= char <= endRange
return char + offset

throw new Error("Char #{char} not in mapping")

for char in [0...64]
map[new BigNumber(char).toString(64)].should.equal(
String.fromCharCode(mapChar(char))
)

it 'should convert ids to URL segments', ->
for [id, urlSegment] in samples
instagramIdToUrlSegment(id).should.equal(urlSegment)
Expand Down
4 changes: 4 additions & 0 deletions test/samples.json
Original file line number Diff line number Diff line change
Expand Up @@ -146,5 +146,9 @@
[
"936303077400215759",
"z-arAqi4DP"
],
[
"2569835492808814665",
"COp48PgD3BJ"
]
]