-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
544 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
Oops, something went wrong.