Skip to content

Commit

Permalink
refactor: rename package @cspell/normalize-json to flatpack-json (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason3S authored Sep 13, 2024
1 parent f671927 commit f4697d0
Show file tree
Hide file tree
Showing 41 changed files with 23,141 additions and 17 deletions.
1 change: 1 addition & 0 deletions cspell-dict.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ deserializers
elisp
estree
exonum
flatpack
gimu
globby
globstar
Expand Down
3 changes: 2 additions & 1 deletion packages/Samples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@
"type": "git",
"url": "https://github.com/streetsidesoftware/cspell.git",
"directory": "packages/Samples"
}
},
"homepage": "https://github.com/streetsidesoftware/cspell/tree/main/packages/Samples#readme"
}
2 changes: 1 addition & 1 deletion packages/cspell-bundled-dicts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"bugs": {
"url": "https://github.com/streetsidesoftware/cspell/labels/cspell-bundled-dicts"
},
"homepage": "https://github.com/streetsidesoftware/cspell#readme",
"homepage": "https://github.com/streetsidesoftware/cspell/tree/main/packages/cspell-bundled-dicts#readme",
"dependencies": {
"@cspell/dict-ada": "^4.0.2",
"@cspell/dict-aws": "^4.0.4",
Expand Down
2 changes: 1 addition & 1 deletion packages/cspell-code-snippets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"bugs": {
"url": "https://github.com/streetsidesoftware/cspell/labels/cspell-trie"
},
"homepage": "https://github.com/streetsidesoftware/cspell#readme",
"homepage": "https://github.com/streetsidesoftware/cspell/tree/main/packages/cspell-code-snippets#readme",
"engines": {
"node": ">=18"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/cspell-config-lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"config"
],
"author": "Jason Dent <[email protected]>",
"homepage": "https://github.com/streetsidesoftware/cspell/tree/main/packages/cspell-config#readme",
"homepage": "https://github.com/streetsidesoftware/cspell/tree/main/packages/cspell-config-lib#readme",
"license": "MIT",
"type": "module",
"sideEffects": false,
Expand Down
2 changes: 1 addition & 1 deletion packages/cspell-dictionary/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"bugs": {
"url": "https://github.com/streetsidesoftware/cspell/labels/cspell-dictionary"
},
"homepage": "https://github.com/streetsidesoftware/cspell#readme",
"homepage": "https://github.com/streetsidesoftware/cspell/tree/main/packages/cspell-dictionary#readme",
"engines": {
"node": ">=18"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/cspell-io/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"bugs": {
"url": "https://github.com/streetsidesoftware/cspell/labels/cspell-io"
},
"homepage": "https://github.com/streetsidesoftware/cspell#readme",
"homepage": "https://github.com/streetsidesoftware/cspell/tree/main/packages/cspell-io#readme",
"engines": {
"node": ">=18"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/cspell-lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"bugs": {
"url": "https://github.com/streetsidesoftware/cspell/labels/cspell-lib"
},
"homepage": "https://github.com/streetsidesoftware/cspell#readme",
"homepage": "https://github.com/streetsidesoftware/cspell/tree/main/packages/cspell-lib#readme",
"dependencies": {
"@cspell/cspell-bundled-dicts": "workspace:*",
"@cspell/cspell-pipe": "workspace:*",
Expand Down
3 changes: 3 additions & 0 deletions packages/cspell-normalize-json/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@
"engines": {
"node": ">=18"
},
"dependencies": {
"flatpack-json": "workspace:*"
},
"devDependencies": {
"@cspell/filetypes": "workspace:*"
}
Expand Down
152 changes: 152 additions & 0 deletions packages/cspell-normalize-json/src/index.test.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
import { readFile } from 'node:fs/promises';

import { findMatchingFileTypes } from '@cspell/filetypes';
import { describe, expect, test } from 'vitest';

import { fromJSON, toJSON } from './index.ts';

const urlFileList = new URL('../fixtures/fileList.txt', import.meta.url);

describe('dehydrate', async () => {
test.each`
data
${undefined}
${'string'}
${1}
${1.1}
${null}
${true}
${false}
${[]}
${[1, 2]}
${['a', 'b', 'a', 'b']}
${{}}
${{ a: 1 }}
${{ a: { b: 1 } }}
${{ a: { a: 'a', b: 42 } }}
${{ a: [1] }}
`('dehydrate/hydrate $data', ({ data }) => {
const v = toJSON(data);
expect(fromJSON(v)).toEqual(data);
});

const biMaxSafe = BigInt(Number.MAX_SAFE_INTEGER);

test.each`
data | options
${undefined} | ${undefined}
${'string'} | ${undefined}
${1} | ${undefined}
${1.1} | ${undefined}
${null} | ${undefined}
${true} | ${undefined}
${false} | ${undefined}
${[]} | ${undefined}
${[1, 2]} | ${undefined}
${['apple', 'banana', 'apple', 'banana', 'apple', 'pineapple']} | ${undefined}
${new Set(['apple', 'banana', 'pineapple'])} | ${undefined}
${new Map([['apple', 1], ['banana', 2], ['pineapple', 3]])} | ${undefined}
${{}} | ${undefined}
${[{}, {}, {}]} | ${undefined}
${{ a: 1 }} | ${undefined}
${{ a: { b: 1 } }} | ${undefined}
${{ a: { a: 'a', b: 42 } }} | ${undefined}
${{ a: [1] }} | ${undefined}
${{ values: ['apple', 'banana', 'pineapple'], set: new Set(['apple', 'banana', 'pineapple']) }} | ${undefined}
${[{ a: 'a', b: 'b' }, { a: 'c', b: 'd' }, { b: 'b', a: 'a' }, ['a', 'b'], ['c', 'd']]} | ${undefined}
${[{ a: 'a', b: 'b' }, { a: 'a', b: 'b' }, { a: 'a', b: 'b' }, { a: 'a', b: 'b' }]} | ${{ dedupe: false }}
${sampleNestedData()} | ${undefined}
${sampleRepeatedStrings()} | ${undefined}
${/[\p{L}\p{M}]+/gu} | ${undefined}
${[/[\p{L}\p{M}]+/gu, /[\p{L}\p{M}]+/gu, /[\p{Lu}\p{M}]+/gu]} | ${undefined}
${[new Date('2024-01-01'), new Date('2024-01-01'), new Date('2024-01-02')]} | ${undefined}
${[1n, 2n, 1n, 2n, biMaxSafe, -biMaxSafe, biMaxSafe + 1n, -biMaxSafe - 1n]} | ${undefined}
${[Object(1n), Object('hello'), Object(/\w+/g), Object(null), Object([]), Object('hello')]} | ${undefined}
`('dehydrate $data $options', ({ data, options }) => {
const v = toJSON(data, { dedupe: options?.dedupe });
expect(v).toMatchSnapshot();
expect(fromJSON(v)).toEqual(data);
expect(fromJSON(JSON.parse(JSON.stringify(v)))).toEqual(data);
});

test.each`
name | data | options
${'fileList'} | ${await sampleFileList()} | ${undefined}
${'fileObjects'} | ${await sampleFileListObjects()} | ${undefined}
`('dehydrate $data $options', ({ name, data, options }) => {
const v = toJSON(data, { dedupe: options?.dedupe });
expect(v).toMatchFileSnapshot(`__snapshots__/${name}.jsonc`);
expect(JSON.stringify(v) + '\n').toMatchFileSnapshot(`__snapshots__/${name}.json`);
expect(JSON.stringify(data) + '\n').toMatchFileSnapshot(`__snapshots__/${name}.data.json`);
expect(fromJSON(v)).toEqual(data);
});

test("make sure dedupe doesn't break Sets", () => {
const data = sampleNestedData();
const value = { ...data, s: new Set(data.n) };

const v = toJSON(value, { dedupe: true });
const hv = fromJSON(v) as typeof value;
expect(hv.s).toEqual(value.s);
expect(hv.n).toEqual(value.n);
});

test("make sure dedupe doesn't break Maps", () => {
const data = sampleNestedData();
const value = { ...data, m: new Map(data.n.map((a) => [a, a])) };

const v = toJSON(value, { dedupe: true });
const hv = fromJSON(v) as typeof value;
expect(hv.m).toEqual(value.m);
expect(hv.n).toEqual(value.n);
});
});

async function sampleFileList() {
const data = await readFile(urlFileList, 'utf8');
const files = data.split('\n');
return files;
}

async function sampleFileListObjects() {
const list = await sampleFileList();
return list.map((filename) => ({ filename, fileType: getFileType(filename) }));
}

function getFileType(filename: string) {
const baseName = filename.split('/').slice(-1).join('');
return findMatchingFileTypes(baseName);
}

function sampleRepeatedStrings() {
const fruit = ['apple', 'banana', 'apple', 'banana', 'apple', 'pineapple'];
const sentence = 'There is a bit of fruit on the table. Some banana, apple, and pineapple.';
const joinFruit = fruit.join('-');
return {
fruit,
joinFruit,
sentence,
};
}

function sampleNestedData() {
const a = { a: 'a', b: 'b' };
const b = { a: 'c', b: 'd' };
const r = /[\p{L}\p{M}]+/gu;
const n = [a, b, { b: 'b', a: 'a' }, ['a', 'b'], ['c', 'd'], r, { r, rr: new RegExp(r) }];
const s = new Set(n);
const m = new Map([
['a', 'a'],
['b', 'b'],
]);
return {
a,
b,
n,
nn: n,
nnn: [...n],
s,
r,
m,
};
}
3 changes: 1 addition & 2 deletions packages/cspell-normalize-json/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export { fromJSON, parse } from './dehydrate.mjs';
export { stringify, toJSON } from './storage.mjs';
export * from 'flatpack-json';
2 changes: 1 addition & 1 deletion packages/cspell-tools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"bugs": {
"url": "https://github.com/streetsidesoftware/cspell/labels/cspell-tools"
},
"homepage": "https://github.com/streetsidesoftware/cspell#readme",
"homepage": "https://github.com/streetsidesoftware/cspell/tree/main/packages/cspell-tools#readme",
"dependencies": {
"@cspell/cspell-pipe": "workspace:*",
"commander": "^12.1.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/cspell-trie-lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"bugs": {
"url": "https://github.com/streetsidesoftware/cspell/labels/cspell-trie"
},
"homepage": "https://github.com/streetsidesoftware/cspell#readme",
"homepage": "https://github.com/streetsidesoftware/cspell/tree/main/packages/cspell-trie-lib#readme",
"dependencies": {
"@cspell/cspell-pipe": "workspace:*",
"@cspell/cspell-types": "workspace:*",
Expand Down
2 changes: 1 addition & 1 deletion packages/cspell-trie/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"bugs": {
"url": "https://github.com/streetsidesoftware/cspell/labels/cspell-trie"
},
"homepage": "https://github.com/streetsidesoftware/cspell#readme",
"homepage": "https://github.com/streetsidesoftware/cspell/tree/main/packages/cspell-trie#readme",
"dependencies": {
"commander": "^12.1.0",
"cspell-trie-lib": "workspace:*",
Expand Down
3 changes: 2 additions & 1 deletion packages/cspell-types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,6 @@
"license": "MIT",
"engines": {
"node": ">=18"
}
},
"homepage": "https://github.com/streetsidesoftware/cspell/tree/main/packages/cspell-types#readme"
}
2 changes: 1 addition & 1 deletion packages/cspell-url/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"repository": {
"type": "git",
"url": "https://github.com/streetsidesoftware/cspell.git",
"directory": "packages/dynamic-import"
"directory": "packages/cspell-url"
},
"bugs": {
"url": "https://github.com/streetsidesoftware/cspell"
Expand Down
2 changes: 1 addition & 1 deletion packages/cspell/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
"bugs": {
"url": "https://github.com/streetsidesoftware/cspell/issues"
},
"homepage": "https://streetsidesoftware.github.io/cspell/",
"homepage": "https://cspell.org/",
"dependencies": {
"@cspell/cspell-json-reporter": "workspace:*",
"@cspell/cspell-pipe": "workspace:*",
Expand Down
16 changes: 16 additions & 0 deletions packages/flatpack-json/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Change Log

All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

## <small>8.14.2 (2024-08-20)</small>

* chore: Update Integration Test Performance Data (#6126) ([012c897](https://github.com/streetsidesoftware/cspell/commit/012c897)), closes [#6126](https://github.com/streetsidesoftware/cspell/issues/6126)

## <small>8.14.1 (2024-08-17)</small>

* fix: Fix publishing ([8a56148](https://github.com/streetsidesoftware/cspell/commit/8a56148))

## 8.14.0 (2024-08-17)

* chore: Update Integration Test Performance Data (#6113) ([c3eb155](https://github.com/streetsidesoftware/cspell/commit/c3eb155)), closes [#6113](https://github.com/streetsidesoftware/cspell/issues/6113)
21 changes: 21 additions & 0 deletions packages/flatpack-json/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Jason Dent

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 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.
17 changes: 17 additions & 0 deletions packages/flatpack-json/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Flatpack JSON

A library to reduce the size of JSON data by flattening and normalizing the values.

It is similar to [flatted](https://www.npmjs.com/package/flatted).

## Install

```sh
npm install -S flatpack-json
```

## Usage

```ts
import { parse, stringify } from 'flatpack-json';
```
Loading

0 comments on commit f4697d0

Please sign in to comment.