Browse Source

Initial commit

Add files from pridepack. Initially supports XML and JSON formats.
master
TheoryOfNekomata 1 year ago
commit
54a2d6230c
12 changed files with 4001 additions and 0 deletions
  1. +9
    -0
      .eslintrc
  2. +109
    -0
      .gitignore
  3. +7
    -0
      LICENSE
  4. +52
    -0
      package.json
  5. +3
    -0
      pridepack.json
  6. +8
    -0
      src/common.ts
  7. +40
    -0
      src/index.ts
  8. +71
    -0
      test/application/json.test.ts
  9. +75
    -0
      test/application/xml.test.ts
  10. +23
    -0
      tsconfig.eslint.json
  11. +23
    -0
      tsconfig.json
  12. +3581
    -0
      yarn.lock

+ 9
- 0
.eslintrc View File

@@ -0,0 +1,9 @@
{
"root": true,
"extends": [
"lxsmnsyc/typescript"
],
"parserOptions": {
"project": "./tsconfig.eslint.json"
}
}

+ 109
- 0
.gitignore View File

@@ -0,0 +1,109 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# 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/

# TypeScript v1 declaration files
typings/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# 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 variables file
.env
.env.production
.env.development

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

# Next.js build output
.next

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# 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

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port

.npmrc
types/
.idea/

+ 7
- 0
LICENSE View File

@@ -0,0 +1,7 @@
MIT License Copyright (c) 2023 TheoryOfNekomata <allan.crisostomo@outlook.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice (including the next paragraph) shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

+ 52
- 0
package.json View File

@@ -0,0 +1,52 @@
{
"name": "@modal-sh/oatmeal",
"version": "0.0.0",
"files": [
"dist",
"src"
],
"engines": {
"node": ">=12"
},
"license": "MIT",
"keywords": [
"pridepack"
],
"devDependencies": {
"@types/node": "^18.14.1",
"eslint": "^8.35.0",
"eslint-config-lxsmnsyc": "^0.5.0",
"pridepack": "2.4.4",
"tslib": "^2.5.0",
"typescript": "^4.9.5",
"vitest": "^0.28.1"
},
"scripts": {
"prepublishOnly": "pridepack clean && pridepack build",
"build": "pridepack build",
"type-check": "pridepack check",
"lint": "pridepack lint",
"clean": "pridepack clean",
"watch": "pridepack watch",
"start": "pridepack start",
"dev": "pridepack dev",
"test": "vitest"
},
"private": false,
"description": "Serialize and deserialize data.",
"repository": {
"url": "https://code.modal.sh/modal-soft/oatmeal",
"type": "git"
},
"homepage": "https://code.modal.sh/modal-soft/oatmeal",
"bugs": {
"url": "https://code.modal.sh/modal-soft/oatmeal/issues"
},
"author": "TheoryOfNekomata <allan.crisostomo@outlook.com>",
"publishConfig": {
"access": "public"
},
"dependencies": {
"xml-js": "^1.6.11"
}
}

+ 3
- 0
pridepack.json View File

@@ -0,0 +1,3 @@
{
"target": "es2018"
}

+ 8
- 0
src/common.ts View File

@@ -0,0 +1,8 @@
export interface TypeOptions {
type: string;
}

export interface SerializeType<Options extends TypeOptions = TypeOptions> {
serialize: <T>(data: T, options: Options) => Promise<string>;
deserialize: <T>(serialized: string, options: Options) => Promise<T>;
}

+ 40
- 0
src/index.ts View File

@@ -0,0 +1,40 @@
import { TypeOptions, SerializeType } from 'src/common';

interface LoadSerializeModule<T extends SerializeType> {
(): Promise<T>;
}

const TYPES: Record<string, LoadSerializeModule<SerializeType>> = {
'application/json': () => import('src/types/application/json'),
'text/json': () => import('src/types/application/json'),
'application/xml': () => import('src/types/application/xml'),
'text/xml': () => import('src/types/application/xml'),
};

export const AVAILABLE_TYPES: (keyof typeof TYPES)[] = Object.keys(TYPES);

export const serialize = async <T, O extends TypeOptions = TypeOptions>(
data: T,
options: O,
): Promise<string> => {
const { type: optionsType } = options;
const { [optionsType]: loadType } = TYPES;
if (!loadType) {
throw new Error(`Unsupported type: ${optionsType}`);
}
const serializeType = await loadType();
return serializeType.serialize(data, options);
};

export const deserialize = async <T, O extends TypeOptions = TypeOptions>(
serialized: string,
options: O,
): Promise<T> => {
const { type: optionsType } = options;
const { [optionsType]: loadType } = TYPES;
if (!loadType) {
throw new Error(`Unsupported type: ${optionsType}`);
}
const serializeType = await loadType();
return serializeType.deserialize<T>(serialized, options);
};

+ 71
- 0
test/application/json.test.ts View File

@@ -0,0 +1,71 @@
import { describe, expect, it } from 'vitest';
import { deserialize, serialize } from '../../src/types/application/json';

describe('application/json', () => {
describe('serialize', () => {
it('should serialize a string', async () => {
const result = await serialize('Hello, World!', { type: 'application/json' });
expect(result).toBe('"Hello, World!"');
});

it('should serialize a number', async () => {
const result = await serialize(123, { type: 'application/json' });
expect(result).toBe('123');
});

it('should serialize a boolean', async () => {
const result = await serialize(true, { type: 'application/json' });
expect(result).toBe('true');
});

it('should serialize an array', async () => {
const result = await serialize([1, 2, 3], { type: 'application/json' });
expect(result).toBe('[1,2,3]');
});

it('should serialize an object', async () => {
const result = await serialize({ a: 1, b: 2, c: 3 }, { type: 'application/json' });
expect(result).toBe('{"a":1,"b":2,"c":3}');
});

it('should serialize an object with indent', async () => {
const result = await serialize({ a: 1, b: 2, c: 3 }, { type: 'application/json', indent: 2 });
expect(result).toBe('{\n "a": 1,\n "b": 2,\n "c": 3\n}');
});
});

describe('deserialize', () => {
it('should deserialize a string', async () => {
const result = await deserialize<string>('"Hello, World!"', { type: 'application/json' });
expect(result).toBe('Hello, World!');
});

it('should deserialize a number', async () => {
const result = await deserialize<number>('123', { type: 'application/json' });
expect(result).toBe(123);
});

it('should deserialize a boolean', async () => {
const result = await deserialize<boolean>('true', { type: 'application/json' });
expect(result).toBe(true);
});

it('should deserialize an array', async () => {
const result = await deserialize<number[]>('[1,2,3]', { type: 'application/json' });
expect(result).toEqual([1, 2, 3]);
});

it('should deserialize an object', async () => {
const result = await deserialize<{ a: number, b: number, c: number }>('{"a":1,"b":2,"c":3}', { type: 'application/json' });
expect(result).toEqual({ a: 1, b: 2, c: 3 });
});

it('should deserialize an object with indent', async () => {
const result = await deserialize<{ a: number, b: number, c: number }>(
'{\n "a": 1,\n "b": 2,\n "c": 3\n}',
{ type: 'application/json' },
);
expect(result).toEqual({ a: 1, b: 2, c: 3 });
});
});
});

+ 75
- 0
test/application/xml.test.ts View File

@@ -0,0 +1,75 @@
import { describe, it, expect } from 'vitest';
import { serialize, deserialize } from '../../src/types/application/xml';

describe('application/xml', () => {
describe('serialize', () => {
it('should serialize a string', async () => {
const result = await serialize('Hello, World!', { type: 'application/xml' });
expect(result).toBe('<root type="string">Hello, World!</root>');
});

it('should serialize a number', async () => {
const result = await serialize(123, { type: 'application/xml' });
expect(result).toBe('<root type="number">123</root>');
});

it('should serialize a boolean', async () => {
const result = await serialize(true, { type: 'application/xml' });
expect(result).toBe('<root type="boolean">true</root>');
});

it('should serialize an array', async () => {
const result = await serialize([1, 2, 3], { type: 'application/xml' });
expect(result).toBe('<root array="array"><_0 type="number">1</_0><_1 type="number">2</_1><_2 type="number">3</_2></root>');
});

it('should serialize an object', async () => {
const result = await serialize({ a: 1, b: 2, c: 3 }, { type: 'application/xml' });
expect(result).toBe('<root type="object"><a type="number">1</a><b type="number">2</b><c type="number">3</c></root>');
});

it('should serialize an object with a custom root element name', async () => {
const result = await serialize({ a: 1, b: 2, c: 3 }, { type: 'application/xml', rootElementName: 'fsh', });
expect(result).toBe('<fsh type="object"><a type="number">1</a><b type="number">2</b><c type="number">3</c></fsh>');
});

it('should serialize an object with indent', async () => {
const result = await serialize({ a: 1, b: 2, c: 3 }, { type: 'application/xml', indent: 2 });
expect(result).toBe(`<root type="object">
<a type="number">1</a>
<b type="number">2</b>
<c type="number">3</c>
</root>`);
});
});

describe('deserialize', () => {
it('should deserialize a string', async () => {
const result = await deserialize<string>('<root type="string">Hello, World!</root>', { type: 'application/xml' });
expect(result).toBe('Hello, World!');
});

it('should deserialize a number', async () => {
const result = await deserialize<number>('<root type="number">123</root>', { type: 'application/xml' });
expect(result).toBe(123);
});

it('should deserialize a boolean', async () => {
const result = await deserialize<boolean>('<root type="boolean">true</root>', { type: 'application/xml' });
expect(result).toBe(true);
});

it('should deserialize an array', async () => {
const result = await deserialize<number[]>('<root array="array"><_0 type="number">1</_0><_1 type="number">2</_1><_2 type="number">3</_2></root>', { type: 'application/xml' });
expect(result).toEqual([1, 2, 3]);
});

it('should deserialize an object', async () => {
const result = await deserialize<{ a: number, b: number, c: number }>(
'<root type="object"><a type="number">1</a><b type="number">2</b><c type="number">3</c></root>',
{ type: 'application/xml' },
);
expect(result).toEqual({ a: 1, b: 2, c: 3 });
});
});
});

+ 23
- 0
tsconfig.eslint.json View File

@@ -0,0 +1,23 @@
{
"exclude": ["node_modules"],
"include": ["src", "types", "test"],
"compilerOptions": {
"module": "ESNext",
"lib": ["ESNext"],
"importHelpers": true,
"declaration": true,
"sourceMap": true,
"rootDir": "./",
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"moduleResolution": "node",
"esModuleInterop": true,
"target": "es2018",
"paths": {
"src/*": ["./src/*"]
}
}
}

+ 23
- 0
tsconfig.json View File

@@ -0,0 +1,23 @@
{
"exclude": ["node_modules"],
"include": ["src", "types"],
"compilerOptions": {
"module": "ESNext",
"lib": ["ESNext"],
"importHelpers": true,
"declaration": true,
"sourceMap": true,
"rootDir": "./src",
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"moduleResolution": "node",
"esModuleInterop": true,
"target": "es2018",
"paths": {
"src/*": ["./src/*"]
}
}
}

+ 3581
- 0
yarn.lock
File diff suppressed because it is too large
View File


Loading…
Cancel
Save