/
githubmirror
/
strapi
Обзор
Документация
Войти
/
githubmirror
/
strapi
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
develop
packages/utils/upgrade/src/modules/json/transform-api.ts
50 строк
1 KB
Christian Capeans
chore: add comments to codemod
08 янв 2024, 15:17
08 янв 2024, 15:17
08147cb
Код
Авторство
О чём код?
import { cloneDeep, get, has, set, merge, omit } from 'lodash/fp'; import type { Utils } from '@strapi/types'; import type { JSONTransformAPI as JSONTransformAPIInterface } from './types'; export class JSONTransformAPI implements JSONTransformAPIInterface { private json: Utils.JSONObject; constructor(json: Utils.JSONObject) { this.json = cloneDeep(json); } get<T extends Utils.JSONValue>(path: string): T | undefined; get<T extends Utils.JSONValue>(path: string, defaultValue: T): T; get<T extends Utils.JSONValue>(path?: string, defaultValue?: T) { if (!path) { return this.root() as T; } return cloneDeep(get(path, this.json) ?? defaultValue) as T; } has(path: string) { return has(path, this.json); } merge(other: Utils.JSONObject) { this.json = merge(other, this.json); return this; } root(): Utils.JSONObject { return cloneDeep(this.json); } set(path: string, value: Utils.JSONValue) { this.json = set(path, value, this.json); return this; } remove(path: string) { this.json = omit(path, this.json); return this; } } export const createJSONTransformAPI = (object: Utils.JSONObject) => new JSONTransformAPI(object);