npm-开发自己的包并发布

目录

1.开发自己的包

1.1. 需要实现的功能

1.2. 初始化包的基本结构 

1.3. 初始化 package.json 

1.4. 在 index.js 中定义格式化时间的方法

1.5. 在 定义转义 和还原HTML 的方法

1.6. 编写包的说明文档

1.7包的入口文件

2.发布自己的包

2.1注册npm账号

2.2登录npm账号

2.3发布并且查看


1.开发自己的包

1.1. 需要实现的功能

格式化日期
转义 HTML 中的 特殊字符
还原 HTML 中的 特殊字符

1.2. 初始化包的基本结构 

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

 

 

1.3. 初始化 package.json 

{
    "name": "itheimazl-tools",
    "version": "1.0.0",
    "main": "index.js",
    "description": "提供了格式化时间,HTMLEscape相关的功能",
    "keywords": [
        "itheimazl",
        "dateFormat",
        "escape"
    ],
    "license": "ISC"
}

1.4. index.js 中定义格式化时间的方法

//定义格式化时间的函数
function dateFormat(dateStr) {
    const dt = new Date(dateStr)
    const y = dt.getFullYear()
    const m = padZero(dt.getMonth() + 1)
    const d = padZero(dt.getDate())
    const hh = padZero(dt.getHours())
    const mm = padZero(dt.getMinutes())
    const ss = padZero(dt.getSeconds())

    return `${y}-${m}-${d} ${hh}:${mm}:${ss}`
}
//定义补零函数
function padZero(n) {
    return n > 9 ? n : '0' + n
}

module.exports = {
    dateFormat
}

1.5. 定义转义 和还原HTML 的方法

//定义转义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(str) {
    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
}

1.6. 编写包的说明文档

## 安装
```
npm install itheimaZL-tools
```

## 导入
```
const itheima = require('itheimaZL-tools')
```

## 格式化时间
```
//调用dateFormat对时间进行格式化
const dtStr = itheima.dateFormat(new Date())
console.log(dtStr);
```

## 转移HTML中的特殊字符
```
const htmlStr = '<h1 title="abc">这是h1标签<span>123&nbsp</span></h1>'
const str = itheima.htmlEscape(htmlStr)
console.log(str)
```

##还原HTML中的特殊字符
```
const str2 = itheima.htmlUnEscape(str)
console.log(str2)
```

1.7包的入口文件

const date = require('./src/dateFormat')
const escape = require('./src/htmlEscape')
//向外面暴露需要的成员
module.exports = {
    ...date,
    ...escape
}

2.发布自己的包

2.1注册npm账号

访问 npm 网站,点击 sign up 按钮,进入注册用户界面

 

 

2.2登录npm账号

npm 账号注册完成后,可以在终端中执行 npm login 命令,依次输入用户名、密码、邮箱后,即可登录成功。

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

2.3发布并且查看

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

江淮-Z

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值