Skip to content

Commit

Permalink
add javascript
Browse files Browse the repository at this point in the history
  • Loading branch information
p9f committed May 23, 2024
1 parent 11c049f commit e51f3db
Show file tree
Hide file tree
Showing 21 changed files with 544 additions and 23 deletions.
Binary file added .coverage
Binary file not shown.
19 changes: 18 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ on:


jobs:
test:
python-test:
runs-on: ubuntu-latest
defaults:
run:
working-directory: python
strategy:
matrix:
version: ["3.10", "3.11", "3.12"]
Expand Down Expand Up @@ -48,10 +51,24 @@ jobs:
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

js-test:
runs-on: ubuntu-latest
defaults:
run:
working-directory: javascript
steps:
- uses: actions/checkout@v4
- uses: oven-sh/setup-bun@v1
- run: bun install
- run: bun test --coverage

release:
needs: test
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags')
runs-on: ubuntu-latest
defaults:
run:
working-directory: frontend
environment:
name: release
url: https://pypi.org/p/dyna-store
Expand Down
175 changes: 175 additions & 0 deletions javascript/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore

# Logs

logs
_.log
npm-debug.log_
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Caches

.cache

# Diagnostic reports (https://nodejs.org/api/report.html)

report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json

# Runtime data

pids
_.pid
_.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover

lib-cov

# Coverage directory used by tools like istanbul

coverage
*.lcov

# nyc test coverage

.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)

.grunt

# Bower dependency directory (https://bower.io/)

bower_components

# node-waf configuration

.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)

build/Release

# Dependency directories

node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)

web_modules/

# TypeScript cache

*.tsbuildinfo

# Optional npm cache directory

.npm

# Optional eslint cache

.eslintcache

# Optional stylelint cache

.stylelintcache

# Microbundle cache

.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history

.node_repl_history

# Output of 'npm pack'

*.tgz

# Yarn Integrity file

.yarn-integrity

# dotenv environment variable files

.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# parcel-bundler cache (https://parceljs.org/)

.parcel-cache

# Next.js build output

.next
out

# Nuxt.js build / generate output

.nuxt
dist

# Gatsby files

# Comment in the public line in if your project uses Gatsby and not Next.js

# https://nextjs.org/blog/next-9-1#public-directory-support

# public

# vuepress build output

.vuepress/dist

# vuepress v2.x temp and cache directory

.temp

# Docusaurus cache and generated files

.docusaurus

# Serverless directories

.serverless/

# FuseBox cache

.fusebox/

# DynamoDB Local files

.dynamodb/

# TernJS port file

.tern-port

# Stores VSCode versions used for testing VSCode extensions

.vscode-test

# yarn v2

.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

# IntelliJ based IDEs
.idea

# Finder (MacOS) folder config
.DS_Store
51 changes: 51 additions & 0 deletions javascript/2
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"templates": {
"a": {
"algorithm": "test",
"campaign_id": null,
"job_id": {
"__hcf": 1,
"i": 9,
"l": 0,
"t": "NoneType"
},
"promoted": false,
"rank": {
"__hcf": 1,
"i": 9,
"l": 0,
"t": "NoneType"
},
"score": {
"__hcf": 1,
"i": 9,
"l": 0,
"t": "NoneType"
},
"timestamp": {
"__hcf": 1,
"i": 3,
"l": 6,
"t": "datetime"
},
"user_id": {
"__hcf": 1,
"i": 0,
"l": 3,
"t": "int"
}
}
},
"ids": {
"a-3BDb2jXPk": {
"algorithm": "test",
"campaign_id": null,
"job_id": null,
"promoted": false,
"rank": null,
"score": null,
"timestamp": "3062-06-30T17:20:21.000Z",
"user_id": 38382
}
}
}
15 changes: 15 additions & 0 deletions javascript/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# dyna-store

To install dependencies:

```bash
bun install
```

To run:

```bash
bun run index.ts
```

This project was created using `bun init` in bun v1.1.4. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime.
Binary file added javascript/bun.lockb
Binary file not shown.
27 changes: 27 additions & 0 deletions javascript/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import data from "../tests/data.json";
import { expect, test } from "bun:test";
import { parse } from ".";

const { ids, templates } = data as Record<
string,
Record<string, Record<string, unknown>>
>;

test("parse is parsing as expected", () => {
for (const [id, expected] of Object.entries(ids)) {
const template = templates[id.split("-")[0]];
const actual = parse(id.split("-")[1], template);
if (typeof expected.timestamp === "string") {
expected.timestamp = new Date(expected.timestamp);
}
for (const key of Object.keys(expected)) {
const value = actual[key];
const expectedValue = expected[key];
if (typeof expectedValue === "number" && typeof value === "number") {
expect(value).toBeCloseTo(expectedValue, 6);
} else {
expect(value).toEqual(expectedValue);
}
}
}
});
72 changes: 72 additions & 0 deletions javascript/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
const BASE62 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

const b62_decode_int = (str: string) => {
const base = BASE62.length;
const strlen = str.length;
let num = 0;
for (let idx = 0; idx < strlen; idx++) {
const power = strlen - (idx + 1);
num += BASE62.indexOf(str[idx]) * base ** power;
}
return num;
};

const b62_decode_np_float_32 = (str: string) => {
const int = b62_decode_int(str);
let buffer = new ArrayBuffer(4);
let view = new DataView(buffer);
view.setUint32(0, int, true);
let floatValue = view.getFloat32(0, true);
return floatValue;
};

type HCFField = {
__hcf: boolean;
i: number;
l: number;
t: "int" | "datetime" | "str" | "NoneType" | "float";
};

const isHCFField = (value: unknown): value is HCFField => {
if (typeof value === "object") {
return value?.hasOwnProperty("__hcf") ?? false;
}
return false;
};

export const parse = (
id: string,
metadata: Record<string, unknown>,
): Record<string, unknown> => {
const toReturn: Record<string, unknown> = {};
for (const key of Object.keys(metadata)) {
const value = metadata[key];
if (isHCFField(value)) {
const encoded_value = id.slice(value.i, value.i + value.l);
let decoded_value;
switch (value.t) {
case "int":
decoded_value = b62_decode_int(encoded_value);
break;
case "datetime":
decoded_value = new Date(b62_decode_int(encoded_value) * 1000);
break;
case "str":
decoded_value = encoded_value;
break;
case "NoneType":
decoded_value = null;
break;
case "float":
decoded_value = b62_decode_np_float_32(encoded_value);
break;
default:
throw new Error(value.t);
}
toReturn[key] = decoded_value;
} else {
toReturn[key] = value;
}
}
return toReturn;
};
11 changes: 11 additions & 0 deletions javascript/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "dyna-store",
"module": "index.ts",
"type": "module",
"devDependencies": {
"@types/bun": "latest"
},
"peerDependencies": {
"typescript": "^5.0.0"
}
}
Loading

0 comments on commit e51f3db

Please sign in to comment.