npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@bob-plug/core

v0.1.6

Published

bob plugin core

Downloads

21

Readme

@bob-plug/core

Bob 核心类型以及部分工具方法集合

特性

  • 根据 Bob 文档封装所有类型;
  • 支持 TypeScript 类型提示;

基础使用

yarn add @bob-plug/core
// main.ts
import * as Bob from '@bob-plug/core';
import { _translate } from './translate';
// https://ripperhe.gitee.io/bob/#/plugin/quickstart/translate
export function translate(query: Bob.TranslateQuery, completion: Bob.Completion) {
  const { text = '', detectFrom, detectTo } = query;
  const params = { from: detectFrom, to: detectTo, cache: Bob.api.getOption('cache') };
  _translate(text, params)
    .then((result) => completion({ result }))
    .catch((error) => {
      Bob.api.$log.error(JSON.stringify(error));
      if (error?.type) return completion({ error });
      completion({ error: Bob.util.error('api', '插件出错', error) });
    });
}

// translate.ts
import * as Bob from '@bob-plug/core';

interface QueryOption {
  to?: Bob.Language;
  from?: Bob.Language;
  cache?: string;
}
var resultCache = new Bob.CacheResult('translate-result');
/**
 * @description 翻译
 * @param {string} text 需要翻译的文字内容
 * @param {object} [options={}]
 * @return {object} 一个符合 bob 识别的翻译结果对象
 */
async function _translate(text: string, options: QueryOption = {}): Promise<Bob.TranslateResult> {
  const { from = 'auto', to = 'auto', cache = 'enable' } = options;
  const cacheKey = `${text}${from}${to}`;
  if (cache === 'enable') {
    const _cacheData = resultCache.get(cacheKey);
    if (_cacheData) return _cacheData;
  } else {
    resultCache.clear();
  }
  const result: Bob.TranslateResult = { from, to, toParagraphs: [] };
  try {
    // 在此处实现翻译的具体处理逻辑
    result.toParagraphs = ['测试文字'];
    result.fromParagraphs = [];
    // result.toDict = { parts: [], phonetics: [] };
  } catch (error) {
    throw Bob.util.error('api', '数据解析错误出错', error);
  }
  if (cache === 'enable') {
    resultCache.set(cacheKey, result);
  }
  return result;
}
export { _translate };

Modules

import { api, util, Cache, CacheResult } from '@bob-plug/core';
  • api Bob 核心内置类

    • api.$data
    • api.$file
    • api.$http
    • api.$info
    • api.$log
    • api.getOption: (key: string) => string 直接使用 $option 时无法获取到用户的配置更新操作;
  • util 工具方法

    • function error(type?: Bob.ServiceErrorType, message?: string, addtion?: any): Bob.ServiceError
    • function isArray(val: any): boolean
    • function isArrayAndLenGt(val: any, len?: number): boolean
    • function isString(val: any): boolean
    • function isPlainObject(val: any): boolean
    • function isNil(val: any): boolean
    • function deepClone(obj: any): any
    • function getType(v: any): string
    • function asyncTo<T, U = any>(promise: Promise<T>, errorExt?: Record<string, unknown>): Promise<[U | null, T | undefined]>
  • Cache 类

    class Cache {
      constructor(nameSpace?: string);
      set(key: string, value: any): void;
      get(key: string): any;
      getAll(): Store;
      remove(key: string): void;
      clear(): void;
    }
  • CacheResult 类

    class CacheResult {
      constructor(nameSpace?: string);
      get(key: string): any;
      set(key: string, val: any): void;
      clear(): void;
    }
  • 其它类型提示相关定义 bob-types.d.ts