开发并发布自己的npm包

一、需要实现的功能

  • 日期格式化
  • html转译
  • html转译后再还原
 // 1,导入自己的包
const ywbTools= require('ywb-tools')
 // ------- 功能1: 格式化日期------
const dt = ywbTools.dateFormat(new Date())
// 输出 2020-01-20 10:09:45
console.log(dt)
// ------- 功能2: 转义 HTML 中的特殊字符-----
const htmlStr = '<h1 style="color: red;">你好! &copy;<span>小黄! </span></h1>'
const str = ywbTools.htmlEscape(htmlstr)
console.log(str)
// &lt;h1 style=&quot;color: red;&quot;&gt;你子! &amp;copy;&lt;span&gt;小黄! &lt;/span&gt;&lt;/h1&gt;
---- 功能3: 还原 HTML 中的特殊字符---
const rawHTML = ywbTools.htmlUnEscape(str)
// 输出 <h1 style="color: red;">你好! &copy;<span>小黄! </span></h1>
console.log(rawHTML)

二、初始化包的基本机构

2.1. 初始化目录结构

  • 新建ywb-tools 文件夹,作为包的根目录
  • 在ywb-tools文件夹中,新建如下三个文件:
    package.json (包管理配置文件)
    index.js (包的入口文件)
    README.md (包的说明文档)

image.png

2.2. 初始化 package.json

{
  "name": "ywb-tools",
  "version": "1.0.1",
  "main": "index.js",
  "description": "提供了格式化时间、HTMLEscape相关功能",
  "keywords": [
    "ywb",
    "dateFormate",
    "escape"
  ],
  "license": "ISC"
}
  • name包名
  • version版本号
  • main入口文件
  • description描述
  • keywords搜索关键词
  • license开源许可协议

三、定义格式化时间的函数

  • 在index.js定义格式化时间的函数
/* 格式化工具 */
function dateFormate(dateStr) {
  const dt = new Date(dateStr);

  const y = dt.getFullYear();
  const m = pdZero(dt.getMonth() + 1);
  const d = pdZero(dt.getDate());

  const hh = pdZero(dt.getHours());
  const mm = pdZero(dt.getMinutes());
  const ss = pdZero(dt.getSeconds());
  return `${y}-${m}-${d} ${hh}:${mm}:${ss}`;
}

/* 补零工具 */

function pdZero(n) {
  return n > 9 ? n : "0" + n;
}
/* 向外暴露成员 */
module.exports = {
  dateFormate,
};
  • 使用
/* 引入方式一 */
const ywbTools = require("../ywb-tools/index");
/* 引入方式二---
1.寻找ywb-tools文件夹下面的package.json
2.寻找package.json的main属性
3.加载main属性的地址 */
const ywbTools = require("../ywb-tools");

// 格式化时间功能
const dtStr = ywbTools.dateFormate(new Date()); //2023-09-10 11:34:16
console.log(dtStr);

四、定义转义 HTML的方法

  • 在indexjs 中定义转义 HTML的方法
/* 定义转译HTML的字符的函数 */

function htmlEscape(htmlStr) {
  return htmlStr.replace(/<|>|"|&/g, (match) => {
    switch (match) {
      case "<":
        return "&lt;";
      case ">":
        return "&gt;";
      case '"':
        return "&quot;";
      case "&":
        return "&amp;";
    }
  });
}

/* 暴露 */
module.exports = {
  dateFormate,
  htmlEscape,
};

  • htmlEscape使用
/* 引入方式一 */
// const ywbTools = require("../ywb-tools/index");
/* 引入方式二---
1.寻找ywb-tools文件夹下面的package.json
2.寻找package.json的main属性
3.加载main属性的地址 */
const ywbTools = require("../ywb-tools");

const htmlStr = '<h1 title="abc">这是h1标签<span>123&nbsp;</span></h1>';
const str = ywbTools.htmlEscape(htmlStr);
// &lt;h1 title=&quot;abc&quot;&gt;这是h1标签&lt;span&gt;123&amp;nbsp;&lt;/span&gt;&lt;/h1&gt;
console.log(str);

五、定义HTML还原方法

  • 在indexjs 中定义HTML还原
/* 定义HTML还原的函数 */

function htmlUnEscape(htmlStr) {
  return htmlStr.replace(/&lt;|&gt;|&quot;|&amp;/g, (match) => {
    switch (match) {
      case "&lt;":
        return "<";
      case "&gt;":
        return ">";
      case "&quot;":
        return '"';
      case "&amp;":
        return "&";
    }
  });
}

/* 暴露 */
module.exports = {
  dateFormate,
  htmlEscape,
  htmlUnEscape,
};

  • htmlUnEscape使用
/* 引入方式一 */
// const ywbTools = require("../ywb-tools/index");
/* 引入方式二---
1.寻找ywb-tools文件夹下面的package.json
2.寻找package.json的main属性
3.加载main属性的地址 */
const ywbTools = require("../ywb-tools");

const str =
  "&lt;h1 title=&quot;abc&quot;&gt;这是h1标签&lt;span&gt;123&amp;nbsp;&lt;/span&gt;&lt;/h1&gt;";
const htmlStr= ywbTools.htmlUnEscape(str);
console.log(htmlStr);
// <h1 title="abc">这是h1标签<span>123&nbsp;</span></h1>

六、将不同的功能进行模块化拆分

  • 将格式化时间的功能,拆分到 src -> dateFormat.js 中
  • 将处理 HTML 字符串的功能,拆分到 src -> htmlEscape.js 中在indexjs 中,*
  • 导入两个模块,得到需要向外共享的方法在indexjs 中,
  • 使用module.exports 把对应的方法共享出去

image.png

6.1. dateFormat.js

/* 格式化工具 */
function dateFormate(dateStr) {
  const dt = new Date(dateStr);

  const y = dt.getFullYear();
  const m = pdZero(dt.getMonth() + 1);
  const d = pdZero(dt.getDate());

  const hh = pdZero(dt.getHours());
  const mm = pdZero(dt.getMinutes());
  const ss = pdZero(dt.getSeconds());
  return `${y}-${m}-${d} ${hh}:${mm}:${ss}`;
}

/* 补零工具 */

function pdZero(n) {
  return n > 9 ? n : "0" + n;
}

module.exports = {
  dateFormate,
};

6.2. htmlEscape.js

/* 定义转译HTML的字符的函数 */

function htmlEscape(htmlStr) {
  return htmlStr.replace(/<|>|"|&/g, (match) => {
    switch (match) {
      case "<":
        return "&lt;";
      case ">":
        return "&gt;";
      case '"':
        return "&quot;";
      case "&":
        return "&amp;";
    }
  });
}

/* 定义HTML还原的函数 */

function htmlUnEscape(htmlStr) {
  return htmlStr.replace(/&lt;|&gt;|&quot;|&amp;/g, (match) => {
    switch (match) {
      case "&lt;":
        return "<";
      case "&gt;":
        return ">";
      case "&quot;":
        return '"';
      case "&amp;":
        return "&";
    }
  });
}
/* 暴露 */
module.exports = {
  htmlEscape,
  htmlUnEscape,
};

6.3. index.js

// 包的入口文件
const date = require("./src/dateFormat");
const escape = require("./src/htmlEscape");
/* 暴露 */
module.exports = {
  ...date,
  ...escape,
};

七、README.md维护

image.png
image.png

八、包发布

8.1. 注册npm账号

  • 访问 https://www.npmjs.com/ 网站,
  • 点击 sign up 按钮,进入注册用户界面 填写账号相关的信息: Full Name、Public Email、Username(不能是纯数字)、 Password
  • 点击 Create an Account 按钮,注册账号
  • 登录邮箱,点击验证链接,进行账号的验证

8.2. 切换为 npm 的官方服务器

注意: 在运行npm login 登录命令之前,必须先把下包的服务器地址切换为 npm 的官方服务器。否则会导致发布包发布失败

  • 方法一:使用nrm镜像源管理工具

image.png

  • 方法二:手动设置下载地址
// 设置官方服务器镜像的地址
npm config set registry https:/registry.npmjs.org/
// 查看当前的下载地址
npm config get registry

8.3. 登录

npm login

8.4. 发布包

找到自己的开发包的根目录运行npm publish

image.png

8.5. 安装包并使用

image.png

  • 可以看到已经安装好了
    image.png
  • 可以看到已经正确安装并使用
    image.png
  • 12
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值