Skip to content

Commit

Permalink
feat: typescript port (#2)
Browse files Browse the repository at this point in the history
* feat: typescript port

* build: workflow jobs
  • Loading branch information
navinkarkera authored Jul 21, 2022
1 parent 169b990 commit 9d3de81
Show file tree
Hide file tree
Showing 14 changed files with 8,406 additions and 1 deletion.
27 changes: 27 additions & 0 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
# For more information see: https://help.github.com/actions/language-and-framework-guides/publishing-nodejs-packages

name: npm Package

on:
release:
types: [created]

jobs:
publish-npm:
runs-on: ubuntu-latest
defaults:
run:
working-directory: ./ts_version
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
cache-dependency-path: ts_version/package-lock.json
node-version: 16
registry-url: https://registry.npmjs.org/
- run: make publish
env:
NODE_AUTH_TOKEN: ${{secrets.npm_token}}
working-directory: ./ts_version

33 changes: 33 additions & 0 deletions .github/workflows/test-ts-version.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: npm test

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

jobs:
test:
runs-on: ubuntu-latest
defaults:
run:
working-directory: ./ts_version
strategy:
matrix:
node-version: [16.x, 17.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache-dependency-path: ts_version/package-lock.json
cache: 'npm'
- run: make install_prereqs
- run: make test

1 change: 0 additions & 1 deletion short_stuff/tests/test_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ def test_e2e_base():
def test_e2e_shortened():
for i in range(100):
thing = gen_shortcode_uuid()
print(f"({repr(thing)}, '{slugify(thing)}')")
assert thing == unslugify(slugify(thing))


Expand Down
20 changes: 20 additions & 0 deletions ts_version/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module.exports = {
root: true,
parser: '@typescript-eslint/parser',
plugins: [
'@typescript-eslint',
],
overrides: [
{
files: ['*.js', '*.jsx', '*.ts', '*.tsx'],
}
],
rules: {
'@typescript-eslint/no-explicit-any': 'off',
},
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
],
};

16 changes: 16 additions & 0 deletions ts_version/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
.idea/**
.vscode/**
node_modules/
.DS_Store
*.swp
coverage/**
.python-version
**/build/**
**/_build/**
docs/assets/**
docs/typedoc/**
docs/providence_docs.egg-info/**
dist/
src/**/*.js
src/**/*.d.ts
src/**/*.map
2 changes: 2 additions & 0 deletions ts_version/.nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
v16.15.0

37 changes: 37 additions & 0 deletions ts_version/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
export PATH := node_modules/.bin:$(PATH)
export SHELL := /usr/bin/env bash
export OS := $(shell uname -s)

.DEFAULT_GOAL := oneshot

install_prereqs:
npm install -D

build: # Build the NPM package.
rm -rvf dist
cp -a src dist
cp tsconfig.json dist/
sed -i -e '/"rootDir"/d' dist/tsconfig.json
cd dist && npx tsc && cd ..
rm -rvf dist/__mocks__
find dist -depth -regex '.*/specs[/]?.*' -delete
cp package.json README.md ../LICENSE.txt dist/

tarball: build
cd dist && npm pack

oneshot: install_prereqs build

quality:
npm run lint

test:
npm run test

publish: install_prereqs build qa
cd dist && npm publish --access public

qa: quality test

fix:
npm run lint:fix
54 changes: 54 additions & 0 deletions ts_version/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Short Stuff

**Typescript port of [short_stuff](https://github.com/Artconomy/short_stuff) built in python.**

ShortStuff is a set of helper functions used for generating short unique
identifiers, such as those used in YouTube video IDs or in URL shorteners.

ShortStuff does this by generating random byte strings, running them through
base64 encoding and then doing some cleanup for compatibility.

Most distinct about the methodology is that it piggy-backs on UUIDs. That means
that if you want to leverage your database or library's native UUID support,
you can do so, and helper functions are provided for this.

---

## Installation

Short Stuff has been tested on node 16 and 17.

To install:

```
npm install shortStuff
```

## Quick Guide

To generate a short code:

```javascript
import { genShortcode } from 'shortStuff'
console.log(genShortcode())
> 'XtFxMb7qTJ-A'
```

To turn this code into a UUID (such as for DB storage):

```javascript
import { unSlugify } from 'shortStuff'
console.log(unSlugify('XtFxMb7qTJ-A'))
> '5ed17131-beea-4c9f-8000-000000000000'
```

Notice that this is a truncated [uuid](https://github.com/uuidjs/uuid), with
everything beyond the first eight bytes zeroed out.

To turn the UUID back into a slug:

```javascript
import { slugify } from 'shortStuff'
console.log(slugify('5ed17131-beea-4c9f-8000-000000000000'))
> 'XtFxMb7qTJ-A'
```
22 changes: 22 additions & 0 deletions ts_version/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
preset: 'ts-jest',
roots: ["src"],
collectCoverageFrom: ['./src/**'],
collectCoverage: true,
coverageThreshold: {
global: {
lines: 90,
}
},
testMatch: [
'**/**/*.test.ts',
],
coveragePathIgnorePatterns: [
'.*[.]spec[.]ts',
'.*[.]d[.]ts',
'/specs/',
'.*[.]js',
]
}

Loading

0 comments on commit 9d3de81

Please sign in to comment.