init report && move live_monitor_vue
This commit is contained in:
2
repo/1-toolkits-helpers/.eslintignore
Normal file
2
repo/1-toolkits-helpers/.eslintignore
Normal file
@@ -0,0 +1,2 @@
|
||||
tsup.config.ts
|
||||
/tests
|
||||
17
repo/1-toolkits-helpers/.eslintrc.cjs
Normal file
17
repo/1-toolkits-helpers/.eslintrc.cjs
Normal file
@@ -0,0 +1,17 @@
|
||||
module.exports = {
|
||||
extends: [
|
||||
'eslint:recommended',
|
||||
'plugin:@typescript-eslint/recommended', // Uses the recommended rules from the @typescript-eslint/eslint-plugin
|
||||
],
|
||||
parserOptions: {
|
||||
ecmaVersion: 2022,
|
||||
sourceType: 'module',
|
||||
project: './tsconfig.json',
|
||||
},
|
||||
rules: {
|
||||
'@typescript-eslint/no-unused-vars': 'off',
|
||||
'@typescript-eslint/no-explicit-any': 'off',
|
||||
'@typescript-eslint/ban-ts-comment': 'off',
|
||||
"@typescript-eslint/ban-types": 'off',
|
||||
},
|
||||
}
|
||||
7
repo/1-toolkits-helpers/.gitignore
vendored
Normal file
7
repo/1-toolkits-helpers/.gitignore
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
/node_modules
|
||||
/dist
|
||||
|
||||
|
||||
|
||||
.env
|
||||
.DS_Store
|
||||
8
repo/1-toolkits-helpers/.npmignore
Normal file
8
repo/1-toolkits-helpers/.npmignore
Normal file
@@ -0,0 +1,8 @@
|
||||
/node_modules
|
||||
/src
|
||||
|
||||
.env
|
||||
.DS_Store
|
||||
|
||||
tsconfig.json
|
||||
tsup.config.js
|
||||
59
repo/1-toolkits-helpers/Readme.md
Normal file
59
repo/1-toolkits-helpers/Readme.md
Normal file
@@ -0,0 +1,59 @@
|
||||
# Простые функции
|
||||
|
||||
## Описание
|
||||
* В данном репозитории собраны простые функции, которые могут пригодиться в разработке. Функции сгруппированны по сущностям, в соответствии с их назначением.
|
||||
## Установка
|
||||
npm install 1-toolkits-helpers --registry=https://vniac.ru:4873/
|
||||
|
||||
### Adapter
|
||||
* Часто используется для адаптации данных, полученных с сервера, к виду, который ожидает фронтенд. Например, переименование ключей, преобразование значений и т.д.
|
||||
#### Функции
|
||||
* convertObject - Адаптирует объект к другому виду, путем замены ключей, может заменять значения через callback
|
||||
* convertList - Имплементация метода convertObject для массива объектов
|
||||
|
||||
```js
|
||||
import {Adapter} from '1-toolkits-helpers'
|
||||
const { convertObject, convertList } = Adapter
|
||||
|
||||
const targetObject = {
|
||||
"id": 1,
|
||||
"created_at": "2024-02-15T17:24:52.755254148+03:00",
|
||||
"updated_at": "2024-02-15T17:24:52.755254148+03:00",
|
||||
"deleted_at": null,
|
||||
"name": "jsonplaceholder.typicode.com",
|
||||
"port": 9965,
|
||||
"proxy_ip": "172.25.78.153",
|
||||
"site_ip": "172.25.78.36",
|
||||
"internet_uri": "localhost",
|
||||
"description": "localhost",
|
||||
"is_online": true
|
||||
}
|
||||
|
||||
const config = {
|
||||
id: "id",
|
||||
name: "name2",
|
||||
port: "port2",
|
||||
proxy_ip: "proxyIp",
|
||||
site_ip: "siteIp",
|
||||
internet_uri: "internetUri",
|
||||
description: "description",
|
||||
is_online: "isOnline",
|
||||
}
|
||||
|
||||
// expectedObject - объект, который мы хотим получить
|
||||
const expectedObject = {
|
||||
id: 1,
|
||||
name2: "jsonplaceholder.typicode.com",
|
||||
port2: 9965,
|
||||
proxyIp: "172.25.78.153",
|
||||
siteIp: "172.25.78.36",
|
||||
internetUri: "localhost",
|
||||
description: "localhost",
|
||||
isOnline: true,
|
||||
}
|
||||
|
||||
const expectedList = [expectedObject]
|
||||
|
||||
convertObject(targetObject, {config})
|
||||
convertList([targetObject], {config})
|
||||
```
|
||||
40
repo/1-toolkits-helpers/package.json
Normal file
40
repo/1-toolkits-helpers/package.json
Normal file
@@ -0,0 +1,40 @@
|
||||
{
|
||||
"name": "1-toolkits-helpers",
|
||||
"description": "",
|
||||
"version": "0.0.5",
|
||||
"main": "./dist/index.js",
|
||||
"module": "./dist/index.mjs",
|
||||
"types": "./dist/index.d.ts",
|
||||
"scripts": {
|
||||
"test": "vitest",
|
||||
"build": "tsup",
|
||||
"release": "npm run build && npm version patch && npm publish --tag latest",
|
||||
"release_up": "npm version $(semver $npm_package_version -i minor) && npm publish --tag latest",
|
||||
"lint": "eslint . --ext .ts",
|
||||
"lint:fix": "eslint . --ext .ts -- --fix"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://gitlab.com/niac_vniizht/niac-packs"
|
||||
},
|
||||
"publishConfig": {
|
||||
"registry": "https://vniac.ru:4873"
|
||||
},
|
||||
"keywords": [
|
||||
"Adapter",
|
||||
"helpers",
|
||||
"utils"
|
||||
],
|
||||
"author": "vbuglov",
|
||||
"license": "ISC",
|
||||
"devDependencies": {
|
||||
"@typescript-eslint/eslint-plugin": "^7.2.0",
|
||||
"@typescript-eslint/parser": "^7.2.0",
|
||||
"eslint": "^8.57.0",
|
||||
"tsup": "^8.0.2",
|
||||
"typescript": "^5.4.2",
|
||||
"typescript-eslint": "^7.2.0",
|
||||
"vite": "^5.1.6",
|
||||
"vitest": "^1.4.0"
|
||||
}
|
||||
}
|
||||
32
repo/1-toolkits-helpers/src/adapter.ts
Normal file
32
repo/1-toolkits-helpers/src/adapter.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
|
||||
/**
|
||||
* Адаптирует объект к другому виду, путем замены ключей, может заменять значения через callback
|
||||
* @param targetObject - целевой объект
|
||||
* @param config - конфигурация конвертации
|
||||
* @param callback - коллбек для дополнительной обработки
|
||||
*/
|
||||
const convertObject = (targetObject={}, {config = {}, callback = (v: any, _v: any) => v}) => {
|
||||
const newObject = {}
|
||||
for (const key in config) {
|
||||
// @ts-ignore
|
||||
newObject[config[key]] = targetObject[key]
|
||||
}
|
||||
return callback(newObject, targetObject)
|
||||
}
|
||||
|
||||
/**
|
||||
* Имплементация метода convertObject для массива объектов
|
||||
* @param targetList
|
||||
* @param config
|
||||
* @param callback
|
||||
*/
|
||||
const convertList = (targetList: Array<Object>, {config = {}, callback = (v: any, _v: any) => v}) => {
|
||||
return targetList.map((targetObject) => convertObject(targetObject, {config, callback}))
|
||||
}
|
||||
|
||||
const Adapter = {
|
||||
convertList,
|
||||
convertObject
|
||||
}
|
||||
|
||||
export default Adapter
|
||||
3
repo/1-toolkits-helpers/src/index.ts
Normal file
3
repo/1-toolkits-helpers/src/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
import Adapter from './adapter'
|
||||
|
||||
export {Adapter}
|
||||
265
repo/1-toolkits-helpers/tests/adapter.test.ts
Normal file
265
repo/1-toolkits-helpers/tests/adapter.test.ts
Normal file
@@ -0,0 +1,265 @@
|
||||
import { expect, test } from 'vitest'
|
||||
import Adapter from '../src/adapter'
|
||||
|
||||
const { convertObject, convertList } = Adapter
|
||||
|
||||
const appBook = {
|
||||
"id": "fb5e7d1d-38cd-4831-bae9-07b36080e3e7",
|
||||
"createdAt": "2024-03-01T23:47:35.711668+03:00",
|
||||
"updatedAt": "2024-03-01T23:47:35.711668+03:00",
|
||||
"deletedAt": null,
|
||||
"owner": "e75aae0d-c1eb-4199-a1d8-2177f57d6a1e",
|
||||
"title": "Тестовая книга",
|
||||
"author": "Васильев А.В.",
|
||||
"description": "test description",
|
||||
"isPublic": false,
|
||||
"publication": null,
|
||||
"imageLink": null,
|
||||
"mapLink": null,
|
||||
"mapParamsId": null,
|
||||
"variables": []
|
||||
}
|
||||
|
||||
const apiBook = {
|
||||
"id": "fb5e7d1d-38cd-4831-bae9-07b36080e3e7",
|
||||
"created_at": "2024-03-01T23:47:35.711668+03:00",
|
||||
"updated_at": "2024-03-01T23:47:35.711668+03:00",
|
||||
"deleted_at": null,
|
||||
"owner": "e75aae0d-c1eb-4199-a1d8-2177f57d6a1e",
|
||||
"title": "Тестовая книга",
|
||||
"author": "Васильев А.В.",
|
||||
"description": "test description",
|
||||
"is_public": false,
|
||||
"publication": null,
|
||||
"image_link": null,
|
||||
"map_link": null,
|
||||
"map_params_id": null,
|
||||
"variables": []
|
||||
}
|
||||
|
||||
test('parse object from api', () => {
|
||||
const targetObject = {
|
||||
"id": 1,
|
||||
"created_at": "2024-02-15T17:24:52.755254148+03:00",
|
||||
"updated_at": "2024-02-15T17:24:52.755254148+03:00",
|
||||
"deleted_at": null,
|
||||
"name": "jsonplaceholder.typicode.com",
|
||||
"port": 9965,
|
||||
"proxy_ip": "172.25.78.153",
|
||||
"site_ip": "172.25.78.36",
|
||||
"internet_uri": "localhost",
|
||||
"description": "localhost",
|
||||
"is_online": true
|
||||
}
|
||||
|
||||
const config = {
|
||||
id: "id",
|
||||
name: "name2",
|
||||
port: "port2",
|
||||
proxy_ip: "proxyIp",
|
||||
site_ip: "siteIp",
|
||||
internet_uri: "internetUri",
|
||||
description: "description",
|
||||
is_online: "isOnline",
|
||||
}
|
||||
|
||||
const expectedObject = {
|
||||
id: 1,
|
||||
name2: "jsonplaceholder.typicode.com",
|
||||
port2: 9965,
|
||||
proxyIp: "172.25.78.153",
|
||||
siteIp: "172.25.78.36",
|
||||
internetUri: "localhost",
|
||||
description: "localhost",
|
||||
isOnline: true,
|
||||
}
|
||||
|
||||
expect(convertObject(targetObject, {config})).toEqual(expectedObject)
|
||||
})
|
||||
|
||||
test('parse book object', () => {
|
||||
const targetObject = {...apiBook}
|
||||
|
||||
const expectedObject = {...appBook}
|
||||
|
||||
const config = {
|
||||
id: 'id',
|
||||
created_at: 'createdAt',
|
||||
updated_at: 'updatedAt',
|
||||
deleted_at: 'deletedAt',
|
||||
owner: 'owner',
|
||||
title: 'title',
|
||||
author: 'author',
|
||||
description: 'description',
|
||||
is_public: 'isPublic',
|
||||
publication: 'publication',
|
||||
image_link: 'imageLink',
|
||||
map_link: 'mapLink',
|
||||
map_params_id: 'mapParamsId',
|
||||
variables: 'variables'
|
||||
}
|
||||
|
||||
const newObject = convertObject(targetObject, {config})
|
||||
|
||||
expect(newObject).toEqual(expectedObject)
|
||||
})
|
||||
|
||||
test('adapt list objects without callback', () => {
|
||||
const targetObject1 = {
|
||||
"id": 1,
|
||||
"created_at": "2024-02-15T17:24:52.755254148+03:00",
|
||||
"updated_at": "2024-02-15T17:24:52.755254148+03:00",
|
||||
"deleted_at": null,
|
||||
"name": "jsonplaceholder.typicode.com",
|
||||
"port": 9965,
|
||||
"proxy_ip": "172.25.78.153",
|
||||
"site_ip": "172.25.78.36",
|
||||
"internet_uri": "localhost",
|
||||
"description": "localhost",
|
||||
"is_online": true
|
||||
}
|
||||
const targetObject2 = {
|
||||
"id": 1,
|
||||
"created_at": "2024-02-15T17:24:52.755254148+03:00",
|
||||
"updated_at": "2024-02-15T17:24:52.755254148+03:00",
|
||||
"deleted_at": null,
|
||||
"name": "jsonplaceholder.typicode.com",
|
||||
"port": 9965,
|
||||
"proxy_ip": "172.25.78.153",
|
||||
"site_ip": "172.25.78.36",
|
||||
"internet_uri": "localhost",
|
||||
"description": "localhost",
|
||||
"is_online": true
|
||||
}
|
||||
const targetObject3 = {
|
||||
"id": 1,
|
||||
"created_at": "2024-02-15T17:24:52.755254148+03:00",
|
||||
"updated_at": "2024-02-15T17:24:52.755254148+03:00",
|
||||
"deleted_at": null,
|
||||
"name": "jsonplaceholder.typicode.com",
|
||||
"port": 9965,
|
||||
"proxy_ip": "172.25.78.153",
|
||||
"site_ip": "172.25.78.36",
|
||||
"internet_uri": "localhost",
|
||||
"description": "localhost",
|
||||
"is_online": true
|
||||
}
|
||||
|
||||
const targetList = [targetObject1, targetObject2, targetObject3]
|
||||
|
||||
const selfObject1 = {
|
||||
id: 1,
|
||||
name2: "jsonplaceholder.typicode.com",
|
||||
port2: 9965,
|
||||
proxy_ip: "172.25.78.153",
|
||||
site_ip: "172.25.78.36",
|
||||
internet_uri: "localhost",
|
||||
description: "localhost",
|
||||
is_online: true,
|
||||
}
|
||||
const selfObject2 = {
|
||||
id: 1,
|
||||
name2: "jsonplaceholder.typicode.com",
|
||||
port2: 9965,
|
||||
proxy_ip: "172.25.78.153",
|
||||
site_ip: "172.25.78.36",
|
||||
internet_uri: "localhost",
|
||||
description: "localhost",
|
||||
is_online: true,
|
||||
}
|
||||
const selfObject3 = {
|
||||
id: 1,
|
||||
name2: "jsonplaceholder.typicode.com",
|
||||
port2: 9965,
|
||||
proxy_ip: "172.25.78.153",
|
||||
site_ip: "172.25.78.36",
|
||||
internet_uri: "localhost",
|
||||
description: "localhost",
|
||||
is_online: true,
|
||||
}
|
||||
|
||||
const selfList = [selfObject1, selfObject2, selfObject3]
|
||||
|
||||
const config = {
|
||||
id: "id",
|
||||
name: "name2",
|
||||
port: "port2",
|
||||
proxy_ip: "proxy_ip",
|
||||
site_ip: "site_ip",
|
||||
internet_uri: "internet_uri",
|
||||
description: "description",
|
||||
is_online: "is_online",
|
||||
}
|
||||
|
||||
expect(convertList(targetList, {config})).toEqual(selfList)
|
||||
})
|
||||
|
||||
test('adapt list objects with callback', () => {
|
||||
const targetObject1 = {
|
||||
"id": 1,
|
||||
"created_at": "2024-02-15T17:24:52.755254148+03:00",
|
||||
"updated_at": "2024-02-15T17:24:52.755254148+03:00",
|
||||
"deleted_at": null,
|
||||
"name": "jsonplaceholder.typicode.com",
|
||||
"port": 9965,
|
||||
"proxy_ip": "172.25.78.153",
|
||||
"site_ip": "172.25.78.36",
|
||||
"internet_uri": "localhost",
|
||||
"description": "localhost",
|
||||
"is_online": true
|
||||
}
|
||||
const targetObject2 = {
|
||||
"id": 1,
|
||||
"created_at": "2024-02-15T17:24:52.755254148+03:00",
|
||||
"updated_at": "2024-02-15T17:24:52.755254148+03:00",
|
||||
"deleted_at": null,
|
||||
"name": "jsonplaceholder.typicode.com",
|
||||
"port": 9965,
|
||||
"proxy_ip": "172.25.78.153",
|
||||
"site_ip": "172.25.78.36",
|
||||
"internet_uri": "localhost",
|
||||
"description": "localhost",
|
||||
"is_online": false
|
||||
}
|
||||
|
||||
const targetList = [targetObject1, targetObject2]
|
||||
|
||||
const expectedObject1 = {
|
||||
id: 1,
|
||||
name2: "jsonplaceholder.typicode.com",
|
||||
port2: 9965,
|
||||
proxy_ip: "172.25.78.153",
|
||||
site_ip: "172.25.78.36",
|
||||
internet_uri: "localhost",
|
||||
description: "localhost",
|
||||
state: "active",
|
||||
}
|
||||
|
||||
const expectedObject2 = {
|
||||
id: 1,
|
||||
name2: "jsonplaceholder.typicode.com",
|
||||
port2: 9965,
|
||||
proxy_ip: "172.25.78.153",
|
||||
site_ip: "172.25.78.36",
|
||||
internet_uri: "localhost",
|
||||
description: "localhost",
|
||||
state: "disabled",
|
||||
}
|
||||
|
||||
|
||||
const expectedList = [expectedObject1, expectedObject2]
|
||||
|
||||
const config = {
|
||||
id: "id",
|
||||
name: "name2",
|
||||
port: "port2",
|
||||
proxy_ip: "proxy_ip",
|
||||
site_ip: "site_ip",
|
||||
internet_uri: "internet_uri",
|
||||
description: "description",
|
||||
}
|
||||
|
||||
const callback = (el, {is_online}) => ({...el, state: is_online ? "active": "disabled"})
|
||||
|
||||
expect(convertList(targetList, {config, callback})).toEqual(expectedList)
|
||||
})
|
||||
18
repo/1-toolkits-helpers/tsconfig.json
Normal file
18
repo/1-toolkits-helpers/tsconfig.json
Normal file
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"noImplicitAny": true,
|
||||
"esModuleInterop": true,
|
||||
"strictNullChecks": true,
|
||||
"moduleResolution": "Node10",
|
||||
"module": "CommonJs",
|
||||
"outDir": "dist",
|
||||
"declaration": true,
|
||||
"isolatedModules": true,
|
||||
"noEmit": true,
|
||||
"include": ["src"],
|
||||
"exclude": ["node_modules"],
|
||||
"format": ["esm"],
|
||||
"compilerOptions": {
|
||||
"strict": true,
|
||||
"target": "ES2022"
|
||||
}
|
||||
}
|
||||
11
repo/1-toolkits-helpers/tsup.config.ts
Normal file
11
repo/1-toolkits-helpers/tsup.config.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
// @ts-nocheck
|
||||
import {defineConfig} from "tsup";
|
||||
|
||||
export default defineConfig({
|
||||
format: ["cjs", "esm"],
|
||||
entry: ['src/index.ts'],
|
||||
dts: true,
|
||||
shims: true,
|
||||
skipNodeModulesBundle: true,
|
||||
clean: true,
|
||||
})
|
||||
Reference in New Issue
Block a user