koishi-utils
Advanced tools
Comparing version 0.1.3 to 0.1.4
@@ -1,3 +0,14 @@ | ||
export declare function escape(source: string, insideCQ?: boolean): string; | ||
export declare function unescape(source: string): string; | ||
export declare function cqCode(type: string, data: Record<string, any>): string; | ||
interface CQCode { | ||
type: string; | ||
data: Record<string, string | number>; | ||
} | ||
declare namespace CQCode { | ||
function escape(source: string, insideCQ?: boolean): string; | ||
function unescape(source: string): string; | ||
function stringify(type: string, data: Record<string, any>): string; | ||
function parse(source: string): { | ||
type: string; | ||
data: Record<string, string | number>; | ||
}; | ||
} | ||
export default CQCode; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
function escape(source, insideCQ = false) { | ||
const result = String(source) | ||
.replace(/&/g, '&') | ||
.replace(/\[/g, '[') | ||
.replace(/\]/g, ']'); | ||
return insideCQ | ||
? result.replace(/,/g, ',').replace(/(\ud83c[\udf00-\udfff])|(\ud83d[\udc00-\ude4f\ude80-\udeff])|[\u2600-\u2B55]/g, ' ') | ||
: result; | ||
} | ||
exports.escape = escape; | ||
function unescape(source) { | ||
return String(source) | ||
.replace(/&/g, '&') | ||
.replace(/[/g, '[') | ||
.replace(/]/g, ']'); | ||
} | ||
exports.unescape = unescape; | ||
function cqCode(type, data) { | ||
return `[CQ:${type}${Object.keys(data).map(key => data[key] ? `,${key}=${escape(data[key], true)}` : '')}]`; | ||
} | ||
exports.cqCode = cqCode; | ||
var CQCode; | ||
(function (CQCode) { | ||
function escape(source, insideCQ = false) { | ||
const result = String(source) | ||
.replace(/&/g, '&') | ||
.replace(/\[/g, '[') | ||
.replace(/\]/g, ']'); | ||
return insideCQ | ||
? result.replace(/,/g, ',').replace(/(\ud83c[\udf00-\udfff])|(\ud83d[\udc00-\ude4f\ude80-\udeff])|[\u2600-\u2B55]/g, ' ') | ||
: result; | ||
} | ||
CQCode.escape = escape; | ||
function unescape(source) { | ||
return String(source) | ||
.replace(/&/g, '&') | ||
.replace(/[/g, '[') | ||
.replace(/]/g, ']') | ||
.replace(/,/g, ','); | ||
} | ||
CQCode.unescape = unescape; | ||
function stringify(type, data) { | ||
let output = '[CQ:' + type; | ||
for (const key in data) { | ||
if (data[key]) | ||
output += `,${key}=${escape(data[key], true)}`; | ||
} | ||
return output + ']'; | ||
} | ||
CQCode.stringify = stringify; | ||
const regexp = /\[CQ:(\w+)((,\w+=[^,\]]*)+)\]/; | ||
function parse(source) { | ||
const result = source.match(regexp); | ||
if (!result) | ||
return null; | ||
const [_, type, attrs] = result; | ||
const data = {}; | ||
attrs.slice(1).split(/,/g).forEach((str) => { | ||
const [_, key, value] = str.match(/^(\w+)=(.+)$/); | ||
data[key] = unescape(value); | ||
}); | ||
return { type, data }; | ||
} | ||
CQCode.parse = parse; | ||
})(CQCode || (CQCode = {})); | ||
exports.default = CQCode; |
@@ -0,8 +1,9 @@ | ||
import CQCode from './cqCode'; | ||
export * from './chinese'; | ||
export * from './cqCode'; | ||
export * from './date'; | ||
export * from './misc'; | ||
export * from './observe'; | ||
export * from './random'; | ||
export * from './set'; | ||
export * from './string'; | ||
export declare function isInteger(source: any): boolean; | ||
export declare function sleep(milliseconds: number): Promise<void>; | ||
export { CQCode }; |
@@ -5,16 +5,14 @@ "use strict"; | ||
} | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const cqCode_1 = __importDefault(require("./cqCode")); | ||
exports.CQCode = cqCode_1.default; | ||
__export(require("./chinese")); | ||
__export(require("./cqCode")); | ||
__export(require("./date")); | ||
__export(require("./misc")); | ||
__export(require("./observe")); | ||
__export(require("./random")); | ||
__export(require("./set")); | ||
__export(require("./string")); | ||
function isInteger(source) { | ||
return typeof source === 'number' && Math.floor(source) === source; | ||
} | ||
exports.isInteger = isInteger; | ||
async function sleep(milliseconds) { | ||
return new Promise(resolve => setTimeout(resolve, milliseconds)); | ||
} | ||
exports.sleep = sleep; |
@@ -10,3 +10,3 @@ "use strict"; | ||
let output = ''; | ||
for (let index = length; index > 0; index--) { | ||
for (let index = length; index > 0; --index) { | ||
output += chars[Math.floor(Math.random() * 62)]; | ||
@@ -13,0 +13,0 @@ } |
{ | ||
"name": "koishi-utils", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"main": "dist/index.js", | ||
@@ -5,0 +5,0 @@ "typings": "dist/index.d.ts", |
Sorry, the diff of this file is not supported yet
170989
22
501