美文网首页
2019-10-17 改造原始的cht查询到的unicodeEn

2019-10-17 改造原始的cht查询到的unicodeEn

作者: 五大RobertWu伍洋 | 来源:发表于2019-10-17 15:49 被阅读0次

search by cht nodejs Chinese to unicode and get below info:

function convert(s) {
  return s.split('').map(function(c) {
    return '\\u' + ('0000' + c.charCodeAt(0).toString(16).toUpperCase()).slice(-4);
  }).join('');
}

var s = '字符串';
s="time out"
const encodeStr = convert(s)
console.log(encodeStr);

refactor the function to my tool js and quick test the selected content in vscode by "ctrl+alt+n" shortcut provided by Code Runner

const unicodeencode = originalStr => {
  return originalStr
    .split("")
    .map(c => {
      return (
        "\\u" +
        (
          "0000" +
          c
            .charCodeAt(0)
            .toString(16)
            .toUpperCase()
        ).slice(-4)
      );
    })
    .join("");
};

const unicodeEncodeTest = inputStr => {
  let s = "字符串";
  let encodeStr = unicodeencode(s);
  console.log(encodeStr);
  s = "time out";
  encodeStr = unicodeencode(s);
  console.log(encodeStr);
};

// unicodeEncodeTest();

now use it in my nodejs cli tool

27617 $ cli --ue wode
{ unicodeencode: 'wode' }
2019-10-17T07:39:28.850Z npmCli key:unicodeencode, value: wode
2019-10-17T07:39:28.850Z npmCli \u0077\u006F\u0064\u0065

the cli configuration is as simple as adding one option now (since I have make it auto call any function name)

  .option("--ue, --unicodeencode [word]", "unicodeencode the string")

here comes the incredible way to make nodejs cli tool auto call any function provided by tool js

const program = require("commander");
....
//filter the program object key to find the user input ones via cli
const checkin = _(program)
  .pickBy((v, p) => {
    return (v === true || _.isString(v)) && !/^_/gi.test(p);
  })
  .value();
//if user input cli option name matches any function name in tool js, then call it
} else if (!_.isEmpty(checkin)) {
  _(checkin).forIn((value, key) => {
    cli.debug(`key:${key}, value: ${value}`);
    clicmd = cli[key];
    const result = clicmd(value);
    if (result) cli.debug(result);
  });
} else {
  program.outputHelp();
  process.exit(1);
}

相关文章

  • 2019-10-17 改造原始的cht查询到的unicodeEn

    search by cht nodejs Chinese to unicode and get below inf...

  • 2019-10-17 refactor cht shared u

    get raw function by cht search refactor it to a function ...

  • 条件查询

    条件查询:根据条件过滤原始表的数据,查询到想要的数据 语法: SELECT 要查询的字段|表达式|常量值|函数 F...

  • MySQL练习题(二)

    原始数据 查询练习 查询student表的第2条到4条记录 查询计算机系和英语系的学生的信息 从student表中...

  • 订单查询改造

    情景: 订单量的日益增长,以及他复杂的查询纬度导致后台管理以及用户端查询订单接口耗时严重,遂对订单查询相关接口进行...

  • 咸阳健康花城

    地砖铺设完成,吊顶交接 水电改造 原始结构

  • 【SpringMVC】MyBatis入门

    1.Mybatis简介 1.1原始jdbc操作(查询数据) 1.2原始jdbc操作(插入数据) 1.3 原始jdb...

  • Mybatis快速入门

    1.Mybatis简介 1.1原始jdbc操作(查询数据) 1.2原始jdbc操作(插入数据) 1.3 原始jdb...

  • django原始sql查询

    row管理器方法用于原始的sql查询,并返回模型的实例 Manager.raw(raw_query, params...

  • 执行原始SQL

    当模型查询API不够用时,您可以退回到编写原始SQL。 Django为您提供了两种执行原始SQL查询的方法:您可以...

网友评论

      本文标题:2019-10-17 改造原始的cht查询到的unicodeEn

      本文链接:https://www.haomeiwen.com/subject/vifvmctx.html