Skip to content

Commit

Permalink
Merge branch 'main' into feat/docs
Browse files Browse the repository at this point in the history
  • Loading branch information
mew-ton committed Feb 5, 2024
2 parents d82682b + 7d5cdbb commit fbdd32d
Show file tree
Hide file tree
Showing 44 changed files with 3,000 additions and 2,006 deletions.
5 changes: 1 addition & 4 deletions .changeset/config.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
{
"$schema": "https://unpkg.com/@changesets/[email protected]/schema.json",
"changelog": [
"@changesets/changelog-github",
{ "repo": "hacomono-lib/json-origami" }
],
"changelog": ["@changesets/changelog-github", { "repo": "hacomono-lib/json-origami" }],
"commit": false,
"access": "restricted",
"baseBranch": "main",
Expand Down
5 changes: 0 additions & 5 deletions .changeset/ninety-pens-suffer.md

This file was deleted.

5 changes: 0 additions & 5 deletions .changeset/warm-teachers-kneel.md

This file was deleted.

17 changes: 15 additions & 2 deletions .github/workflows/codspeed.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
---
name: Codspeed Benchmarks
run-name: |-
github.event_name == 'pull_request' &&
"Test on ${{ github.event.pull_request.title }} by ${{ github.actor }}" ||
"Test on main"
permissions:
contents: read
Expand All @@ -10,12 +15,20 @@ on:
branches:
- "main"
pull_request:
types: [opened, synchronize, reopened, ready_for_review]
paths:
- ".github/workflows/codspeed.yml"
- ".gitignore"
- "src/**"
- "test/**"
- "package.json"
- "vitest.config.ts"
- "yarn.lock"

jobs:
init__node:
if: |
!contains(github.event.pull_request.labels.*.name, 'renovate')
name: "Initialize: node"
!contains(github.event.pull_request.labels.*.name, 'renovate') && github.event.pull_request.title != 'Version Packages'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand Down
11 changes: 4 additions & 7 deletions .github/workflows/lint__codebase.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
name: Lint Code Base
run-name: "Lint code base (eslint) on \"${{ github.event.pull_request.title }}\" by ${{ github.actor }}"
name: Lint Code Base (Biome)
run-name: "Lint code base (Biome) on \"${{ github.event.pull_request.title }}\" by ${{ github.actor }}"

permissions:
contents: read
Expand All @@ -12,12 +12,9 @@ on:
types: [opened, synchronize]

jobs:
eslint:
name: "Lint: ESLint"
biome:
if: |
(contains(github.event.pull_request.labels.*.name, 'renovate') &&
contains(github.event.pull_request.labels.*.name, 'renovate:lint')) ||
contains(github.event.pull_request.labels.*.name, 'renovate') == false
!contains(github.event.pull_request.labels.*.name, 'renovate')
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ jobs:
publish: yarn run changeset publish
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
NPM_TOKEN: ${{ secrets.GITHUB_TOKEN }}
8 changes: 4 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ on:
paths:
- ".github/workflows/test.yml"
- ".gitignore"
- "src"
- "src/**"
- "test/**"
- "package.json"
- "tsconfig.json"
- "tsup.config.ts"
Expand All @@ -23,9 +24,8 @@ on:
jobs:
init__node:
if: |
(contains(github.event.pull_request.labels.*.name, 'renovate') &&
!contains(github.event.pull_request.labels.*.name, 'renovate:lint')) ||
!contains(github.event.pull_request.labels.*.name, 'renovate')
!contains(github.event.pull_request.labels.*.name, 'renovate') ||
github.event.pull_request.title != 'Version Packages'
name: "Initialize: node"
runs-on: ubuntu-latest
steps:
Expand Down
672 changes: 336 additions & 336 deletions .yarn/releases/yarn-4.0.2.cjs → .yarn/releases/yarn-4.1.0.cjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion .yarnrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ npmAuthToken: "${NODE_AUTH_TOKEN:-''}"

npmPublishRegistry: 'https://registry.npmjs.org/'

yarnPath: .yarn/releases/yarn-4.0.2.cjs
yarnPath: .yarn/releases/yarn-4.1.0.cjs
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
# json-origami

## 0.5.0

### Minor Changes

- [#109](https://github.com/hacomono-lib/json-origami/pull/109) [`655d4f7`](https://github.com/hacomono-lib/json-origami/commit/655d4f79683c5c9d2c4d8472f755384a7644753d) Thanks [@mew-ton](https://github.com/mew-ton)! - fix: performance improvement (#106)

### Patch Changes

- [#107](https://github.com/hacomono-lib/json-origami/pull/107) [`0054bcb`](https://github.com/hacomono-lib/json-origami/commit/0054bcb8f1cd80252cb09d7b13a4dc92a38bd216) Thanks [@mew-ton](https://github.com/mew-ton)! - unfold 関数にて、値が空配列・空オブジェクトのときに値が結果に反映されない問題の修正

- [#125](https://github.com/hacomono-lib/json-origami/pull/125) [`27733a4`](https://github.com/hacomono-lib/json-origami/commit/27733a4ece3ba998f07e0fa256fd088ed04bc671) Thanks [@mew-ton](https://github.com/mew-ton)! - misc: codspeed がうまく連携できてなかたt

- [#122](https://github.com/hacomono-lib/json-origami/pull/122) [`4a742c7`](https://github.com/hacomono-lib/json-origami/commit/4a742c7b9bc054bc0bb333c10c0b77cb1dbc905f) Thanks [@renovate](https://github.com/apps/renovate)! - fix: update dependency type-fest to ^4.10.1

- [#115](https://github.com/hacomono-lib/json-origami/pull/115) [`691dd55`](https://github.com/hacomono-lib/json-origami/commit/691dd55f097fa275b4d42bd67e7806702cdc8b23) Thanks [@mew-ton](https://github.com/mew-ton)! - misc: replace eslint -> biome, pre-commit -> lefthook

## 0.4.1

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion biome.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "https://biomejs.dev/schemas/1.4.1/schema.json",
"files": {
"ignore": ["node_modules", "coverage", ".astro"]
"ignore": ["node_modules", "coverage", ".astro", ".yarn"]
},
"organizeImports": {
"enabled": true
Expand Down
17 changes: 11 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "json-origami",
"version": "0.4.1",
"version": "0.5.0",
"description": "Transform and reshape your JSON objects with the artistry of origami.",
"keywords": [
"key-value",
Expand All @@ -15,7 +15,7 @@
},
"author": "mew-ton <[email protected]>",
"license": "MIT",
"packageManager": "yarn@4.0.2",
"packageManager": "yarn@4.1.0",
"type": "module",
"files": [
"dist",
Expand All @@ -36,29 +36,34 @@
"access": "public"
},
"scripts": {
"dev": "vitest --ui",
"dev": "yarn vitest --ui",
"bench": "vitest bench",
"doc": "astro dev --root ./docs --open",
"build": "tsup",
"doc-build": "astro check --root ./docs && astro build --root ./docs",
"test": "run-p test:*",
"test:spec": "vitest --run --silent",
"test:spec": "yarn vitest --run --silent",
"test:type": "tsc --noEmit -p tsconfig.json",
"vitest": "node --expose-gc node_modules/.bin/vitest",
"format": "biome format --write ./src ./test",
"lint": "biome check --apply ./src ./test"
},
"dependencies": {
"type-fest": "^4.10.2"
},
"devDependencies": {
"@astrojs/check": "^0.4.1",
"@astrojs/starlight": "^0.17.0",
"@biomejs/biome": "^1.5.3",
"@changesets/changelog-github": "^0.5.0",
"@changesets/cli": "^2.27.1",
"@codspeed/vitest-plugin": "^3.1.0",
"@types/node": "^20.11.16",
"@vitest/ui": "^1.2.2",
"astro": "^4.2.6",
"defu": "^6.1.4",
"sharp": "^0.33.2",
"tsup": "^8.0.1",
"type-fest": "^4.9.0",
"typescript": "^5.3.3",
"vitest": "^1.2.2",
"yarn-run-all": "latest"
Expand All @@ -73,6 +78,6 @@
},
"volta": {
"node": "20.11.0",
"yarn": "4.0.2"
"yarn": "4.1.0"
}
}
50 changes: 17 additions & 33 deletions src/fold.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
import {
type ArrayIndex,
type Dictionary,
type FixedFoldOption,
type FoldOption,
type Folded,
type Primitive,
defaultCommonOption,
} from './type'
import { toModifier } from './lib'
import { type Dictionary, type FixedFoldOption, type FoldOption, type Folded, defaultCommonOption } from './type'

/**
* Fold an object into a one-level object.
Expand Down Expand Up @@ -37,34 +30,25 @@ export function fold<D extends Dictionary>(obj: D, option?: FoldOption): Folded<
return {}
}

return Object.fromEntries(
flatEntries(option?.keyPrefix ?? '', obj, {
...defaultCommonOption,
...option,
} as FixedFoldOption),
) as Folded<D>
}

const arrayKeyMap = {
dot: (prefix: string, index: number) => (prefix === '' ? `${index}` : `${prefix}.${index}`),
bracket: (prefix: string, index: number) => `${prefix}[${index}]`,
} satisfies Record<ArrayIndex, (prefix: string, index: number) => string>
const fixedOption = {
...defaultCommonOption,
...option,
} as FixedFoldOption

function flatEntries(key: string, value: object, opt: FixedFoldOption): [string, Primitive][] {
if (value === undefined || value === null) {
return []
}
const modifier = toModifier(obj, fixedOption)

const appendKey = (k: string | number) =>
typeof k === 'number' ? arrayKeyMap[opt.arrayIndex](key, k) : key === '' ? k : `${key}.${k}`
const result = {} as Folded<D>

if (Array.isArray(value) && value.length > 0) {
return value.flatMap((v, i) => flatEntries(appendKey(i), v, opt))
for (const [key, value] of modifier.entries()) {
result[fixKey(fixedOption, key)] = value
}

if (typeof value === 'object' && Object.keys(value).length > 0) {
return Object.entries(value as object).flatMap(([k, v]) => flatEntries(appendKey(k), v, opt))
}
return result
}

return [[key, value]]
function fixKey(option: FixedFoldOption, key: string) {
if (option.keyPrefix) {
return key.startsWith('[') ? `${option.keyPrefix}${key}` : `${option.keyPrefix}.${key}`
}
return key
}
2 changes: 2 additions & 0 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { createEmptyModifier, toModifier, toRaw } from './modifier'
export { startsKeyWith } from './string'
Loading

0 comments on commit fbdd32d

Please sign in to comment.