Skip to content

Commit

Permalink
feat(utils): add CQCode.stringifyAll and CQCode.parseAll
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jan 7, 2020
1 parent 96d694b commit 764b816
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 48 deletions.
15 changes: 6 additions & 9 deletions build/bump.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { writeJson } from 'fs-extra'
import { resolve } from 'path'
import { SemVer, gt } from 'semver'
import { cyan, green } from 'kleur'
import { PackageJson } from './utils'
import latest from 'latest-version'
import globby from 'globby'
import CAC from 'cac'
Expand All @@ -15,17 +16,9 @@ const { args, options } = CAC()

type BumpType = 'major' | 'minor' | 'patch' | 'auto'

interface PackageJSON {
name: string
private?: boolean
version: string
dependencies: Record<string, string>
devDependencies: Record<string, string>
}

class Package {
name: string
meta: PackageJSON
meta: PackageJson
oldVersion: string
version: SemVer
dirty: boolean
Expand Down Expand Up @@ -113,6 +106,10 @@ function bumpPkg (source: Package, flag: BumpType, depth: number) {
Object.keys(meta.devDependencies || {}).forEach((name) => {
if (name !== source.name) return
meta.devDependencies[name] = '^' + newVersion
})
Object.keys(meta.peerDependencies || {}).forEach((name) => {
if (name !== source.name) return
meta.peerDependencies[name] = '^' + newVersion
dependents.add(target)
})
Object.keys(meta.dependencies || {}).forEach((name) => {
Expand Down
8 changes: 4 additions & 4 deletions packages/database-level/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "koishi-database-level",
"description": "Leveldb support for Koishi",
"version": "1.0.2",
"version": "1.0.3",
"main": "dist/index.js",
"files": [
"dist"
Expand Down Expand Up @@ -33,13 +33,13 @@
"leveldb"
],
"devDependencies": {
"koishi-test-utils": "^1.0.2"
"koishi-test-utils": "^1.0.3"
},
"dependencies": {
"@types/leveldown": "^4.0.2",
"@types/levelup": "^4.3.0",
"koishi-core": "^1.1.0",
"koishi-utils": "^1.0.1",
"koishi-core": "^1.1.1",
"koishi-utils": "^1.0.2",
"leveldown": "^5.4.1",
"levelup": "^4.3.2",
"subleveldown": "^4.1.4"
Expand Down
6 changes: 3 additions & 3 deletions packages/database-mysql/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "koishi-database-mysql",
"description": "MySQL support for Koishi",
"version": "1.0.2",
"version": "1.0.3",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"files": [
Expand Down Expand Up @@ -35,8 +35,8 @@
"@types/mysql": "^2.15.8"
},
"dependencies": {
"koishi-core": "^1.1.0",
"koishi-utils": "^1.0.1",
"koishi-core": "^1.1.1",
"koishi-utils": "^1.0.2",
"mysql": "^2.17.1"
}
}
8 changes: 4 additions & 4 deletions packages/koishi-cli/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "koishi",
"description": "A QQ bot framework based on CQHTTP",
"version": "1.1.0",
"version": "1.1.1",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"files": [
Expand Down Expand Up @@ -38,9 +38,9 @@
"dependencies": {
"cac": "^6.5.3",
"kleur": "^3.0.3",
"koishi-core": "^1.1.0",
"koishi-plugin-common": "^1.0.2",
"koishi-utils": "^1.0.1",
"koishi-core": "^1.1.1",
"koishi-plugin-common": "^1.0.3",
"koishi-utils": "^1.0.2",
"prompts": "^2.3.0"
}
}
8 changes: 4 additions & 4 deletions packages/koishi-core/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "koishi-core",
"description": "Core features for Koishi",
"version": "1.1.0",
"version": "1.1.1",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"files": [
Expand Down Expand Up @@ -34,14 +34,14 @@
"devDependencies": {
"@types/debug": "^4.1.5",
"@types/ws": "^6.0.4",
"koishi-database-level": "^1.0.1",
"koishi-test-utils": "^1.0.1"
"koishi-database-level": "^1.0.3",
"koishi-test-utils": "^1.0.3"
},
"dependencies": {
"axios": "^0.19.0",
"debug": "^4.1.1",
"escape-string-regexp": "^2.0.0",
"koishi-utils": "^1.0.0",
"koishi-utils": "^1.0.2",
"leven": "^3.1.0",
"ws": "^7.2.1"
}
Expand Down
2 changes: 1 addition & 1 deletion packages/koishi-utils/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "koishi-utils",
"description": "Utilities for Koishi",
"version": "1.0.1",
"version": "1.0.2",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"files": [
Expand Down
39 changes: 31 additions & 8 deletions packages/koishi-utils/src/cqCode.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
type CQCodeData = Record<string, string | number>

interface CQCode {
type: string
data: Record<string, string | number>
data: CQCodeData
capture?: RegExpMatchArray
}

namespace CQCode {
export function escape (source: string, insideCQ = false) {
export function escape (source: any, insideCQ = false) {
const result = String(source)
.replace(/&/g, '&amp;')
.replace(/\[/g, '&#91;')
Expand All @@ -22,26 +25,46 @@ namespace CQCode {
.replace(/&amp;/g, '&')
}

export function stringify (type: string, data: Record<string, any>) {
export function stringify (type: string, data: CQCodeData) {
if (type === 'text') return data.text
let output = '[CQ:' + type
for (const key in data) {
if (data[key]) output += `,${key}=${escape(data[key], true)}`
}
return output + ']'
}

export function stringifyAll (codes: CQCode[]) {
return codes.map(code => stringify(code.type, code.data)).join('')
}

const regexp = /\[CQ:(\w+)((,\w+=[^,\]]*)+)\]/

export function parse (source: string) {
const result = source.match(regexp)
if (!result) return null
const [_, type, attrs] = result
export function parse (source: string): CQCode {
const capture = source.match(regexp)
if (!capture) return null
const [_, type, attrs] = capture
const data: Record<string, string | number> = {}
attrs.slice(1).split(/,/g).forEach((str) => {
const [_, key, value] = str.match(/^(\w+)=(.+)$/)
data[key] = unescape(value)
})
return { type, data }
return { type, data, capture }
}

export function parseAll (source: string) {
const codes: CQCode[] = []
let result: CQCode
while ((result = parse(source))) {
const { capture } = result
if (capture.index) {
codes.push({ type: 'text', data: { text: source.slice(0, capture.index) } })
}
codes.push(result)
source = source.slice(capture.index + capture[0].length)
}
if (source) codes.push({ type: 'text', data: { text: source } })
return codes
}
}

Expand Down
30 changes: 30 additions & 0 deletions packages/koishi-utils/tests/cqCode.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,41 @@ describe('CQ Code', () => {
expect(CQCode.stringify('share', { title: '', url: 'https://test.com', content: 'Hello' })).toBe('[CQ:share,url=https://test.com,content=Hello]')
})

test('stringifyAll', () => {
expect(CQCode.stringifyAll([{
type: 'text',
data: { text: 'foo' },
}, {
type: 'bar',
data: { text: 'bar' },
}, {
type: 'text',
data: { text: 'baz' },
}])).toBe('foo[CQ:bar,text=bar]baz')
})

test('parse', () => {
expect(CQCode.parse('[CQ:image,')).toBeNull()
expect(CQCode.parse('[CQ:image,file=https://test.com/?foo=1&amp;bar=2&#44;3]')).toMatchObject({
type: 'image',
data: { file: 'https://test.com/?foo=1&bar=2,3' },
})
})

test('parseAll', () => {
expect(CQCode.parseAll('foo[CQ:bar,text=bar]')).toMatchObject([{
type: 'text',
data: { text: 'foo' },
}, {
type: 'bar',
data: { text: 'bar' },
}])
expect(CQCode.parseAll('[CQ:bar,text=bar]baz')).toMatchObject([{
type: 'bar',
data: { text: 'bar' },
}, {
type: 'text',
data: { text: 'baz' },
}])
})
})
6 changes: 3 additions & 3 deletions packages/plugin-common/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "koishi-plugin-common",
"description": "Common plugins for Koishi",
"version": "1.0.2",
"version": "1.0.3",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"author": "Shigma <1700011071@pku.edu.cn>",
Expand All @@ -28,7 +28,7 @@
"plugin"
],
"dependencies": {
"koishi-core": "^1.1.0",
"koishi-utils": "^1.0.1"
"koishi-core": "^1.1.1",
"koishi-utils": "^1.0.2"
}
}
8 changes: 4 additions & 4 deletions packages/plugin-schedule/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "koishi-plugin-schedule",
"description": "Schedule plugin for Koishi",
"version": "0.1.0",
"version": "0.1.1",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"author": "Shigma <1700011071@pku.edu.cn>",
Expand Down Expand Up @@ -30,10 +30,10 @@
"task"
],
"devDependencies": {
"koishi-database-level": "^1.0.2",
"koishi-database-mysql": "^1.0.2"
"koishi-database-level": "^1.0.3",
"koishi-database-mysql": "^1.0.3"
},
"dependencies": {
"koishi-core": "^1.1.0"
"koishi-core": "^1.1.1"
}
}
10 changes: 5 additions & 5 deletions packages/plugin-teach/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "koishi-plugin-teach",
"description": "Teach plugin for Koishi",
"version": "0.1.8",
"version": "0.1.9",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"author": "Shigma <1700011071@pku.edu.cn>",
Expand Down Expand Up @@ -31,11 +31,11 @@
"conversation"
],
"devDependencies": {
"koishi-database-level": "^1.0.2",
"koishi-database-mysql": "^1.0.2"
"koishi-database-level": "^1.0.3",
"koishi-database-mysql": "^1.0.3"
},
"dependencies": {
"koishi-core": "^1.1.0",
"koishi-utils": "^1.0.1"
"koishi-core": "^1.1.1",
"koishi-utils": "^1.0.2"
}
}
6 changes: 3 additions & 3 deletions packages/test-utils/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "koishi-test-utils",
"description": "Test utilities for Koishi",
"version": "1.0.2",
"version": "1.0.3",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"files": [
Expand Down Expand Up @@ -39,7 +39,7 @@
"axios": "^0.19.0",
"debug": "^4.1.1",
"express": "^4.17.1",
"koishi-core": "^1.1.0",
"koishi-utils": "^1.0.1"
"koishi-core": "^1.1.1",
"koishi-utils": "^1.0.2"
}
}

0 comments on commit 764b816

Please sign in to comment.