Skip to content

Commit

Permalink
chore: add @koishijs/segment
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Feb 17, 2022
1 parent e95802b commit 764638a
Show file tree
Hide file tree
Showing 13 changed files with 73 additions and 13 deletions.
1 change: 1 addition & 0 deletions .mocharc.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const specs = [
'community/chai-shape/tests/*.spec.ts',
'community/schemastery/tests/*.spec.ts',
'packages/core/tests/*.spec.ts',
'packages/segment/tests/*.spec.ts',
'packages/utils/tests/*.spec.ts',
'plugins/a11y/admin/tests/*.spec.ts',
'plugins/a11y/rate-limit/tests/*.spec.ts',
Expand Down
2 changes: 1 addition & 1 deletion build/dtsc.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable no-cond-assign */

import { spawnAsync, cwd, getPackages, PackageJson, requireSafe } from './utils'
import { cwd, getPackages, PackageJson, requireSafe, spawnAsync } from './utils'
import { EOL } from 'os'
import { resolve } from 'path'
import fs from 'fs-extra'
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"chai-shape": "^1.0.0"
},
"dependencies": {
"@koishijs/utils": "^5.0.1",
"@koishijs/utils": "^5.1.0",
"fastest-levenshtein": "^1.0.12",
"schemastery": "^2.4.2"
}
Expand Down
2 changes: 1 addition & 1 deletion packages/koishi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"dependencies": {
"@koa/router": "^10.1.1",
"@koishijs/core": "^4.2.2",
"@koishijs/utils": "^5.0.1",
"@koishijs/utils": "^5.1.0",
"@types/koa": "*",
"@types/koa__router": "*",
"@types/ws": "^8.2.2",
Expand Down
30 changes: 30 additions & 0 deletions packages/segment/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "@koishijs/segment",
"description": "Segment Manipulation",
"version": "1.0.0",
"main": "lib/node.js",
"module": "lib/browser.js",
"typings": "lib/index.d.ts",
"files": [
"lib"
],
"author": "Shigma <shigma10826@gmail.com>",
"license": "MIT",
"repository": {
"type": "git",
"url": "git+https://github.com/koishijs/koishi.git"
},
"bugs": {
"url": "https://github.com/koishijs/koishi/issues"
},
"homepage": "https://koishi.js.org/api/utils.html",
"keywords": [
"bot",
"qqbot",
"cqhttp",
"coolq",
"chatbot",
"koishi",
"utilities"
]
}
24 changes: 19 additions & 5 deletions packages/utils/src/segment.ts → packages/segment/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
import { Awaitable, Dict, isType } from './misc'
type Dict<T = any> = { [key: string]: T }
type Awaitable<T> = [T] extends [Promise<unknown>] ? T : T | Promise<T>

type Global = NodeJS.Global & Window & typeof globalThis

type GlobalClass = {
[K in keyof Global]: Global[K] extends new (...args: any[]) => infer T ? T : never
}

const root: any = typeof self !== 'undefined' ? self : global

function isType<K extends keyof GlobalClass>(type: K, value: any): value is GlobalClass[K] {
return type in root && value instanceof root[type]
|| Object.prototype.toString.call(value).slice(8, -1) === type
}

// eslint-disable-next-line @typescript-eslint/naming-convention
export interface segment {
interface segment {
type: string
data: segment.Data
}

export function segment(type: string, data: segment.Data = {}) {
function segment(type: string, data: segment.Data = {}) {
if (type === 'text') return segment.escape(String(data.content))
let output = '[CQ:' + type
for (const key in data) {
Expand All @@ -18,7 +32,7 @@ export function segment(type: string, data: segment.Data = {}) {
// eslint-disable-next-line @typescript-eslint/naming-convention
type primitive = string | number | boolean

export namespace segment {
namespace segment {
export type Chain = segment.Parsed[]
export type Data = Dict<primitive>
export type Transformer = string | ((data: Dict<string>, index: number, chain: Chain) => string)
Expand Down Expand Up @@ -134,4 +148,4 @@ export namespace segment {
export const file = createAssetFactory('file')
}

export { segment as s }
export = segment
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { segment } from 'koishi'
import { expect } from 'chai'
import {} from 'chai-shape'

describe('Segment API', () => {
it('segment.escape()', () => {
Expand Down
10 changes: 10 additions & 0 deletions packages/segment/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "../../tsconfig.base",
"compilerOptions": {
"rootDir": "src",
"outDir": "lib",
},
"include": [
"src"
]
}
3 changes: 2 additions & 1 deletion packages/utils/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@koishijs/utils",
"description": "Utilities for Koishi",
"version": "5.0.1",
"version": "5.1.0",
"main": "lib/node.js",
"module": "lib/browser.js",
"typings": "lib/index.d.ts",
Expand Down Expand Up @@ -32,6 +32,7 @@
"@types/supports-color": "^8.1.1"
},
"dependencies": {
"@koishijs/segment": "^1.0.0",
"supports-color": "^8.1.0"
}
}
5 changes: 4 additions & 1 deletion packages/utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import segment from '@koishijs/segment'

export { segment, segment as s }

export * from './logger'
export * from './misc'
export * from './observe'
export * from './random'
export * from './segment'
export * from './string'
export * from './time'
2 changes: 1 addition & 1 deletion plugins/frontend/client/client/components/chat/content.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

<script lang="ts" setup>
import { segment } from '@koishijs/utils'
import segment from '@koishijs/segment'
import KImage from './image.vue'
defineProps<{
Expand Down
2 changes: 1 addition & 1 deletion plugins/frontend/client/client/components/chat/panel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<script lang="ts" setup>
import { ref } from 'vue'
import { segment } from '@koishijs/utils'
import segment from '@koishijs/segment'
import VirtualList from './list.vue'
const emit = defineEmits(['send', 'click', 'paste', 'update:activeKey'])
Expand Down
2 changes: 1 addition & 1 deletion plugins/frontend/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"build"
],
"dependencies": {
"@koishijs/components": "^1.0.0",
"@koishijs/segments": "^1.0.0",
"@types/marked": "^4.0.1",
"@vitejs/plugin-vue": "^2.0.1",
"@vueuse/core": "^7.5.3",
Expand Down

0 comments on commit 764638a

Please sign in to comment.