Novice Teaching - teach to write a bag hand in hand

Keywords: node.js

To write a package

First, you need to register the npm website account:

npmhttps://www.npmjs.com/ Go to the npm website and click sign   Press the up button to enter the registration user interface

Fill in the information related to the account:   full   Name,   Public   Email,   Username,   Password

Click Create   an   Account button,   Registered account

Log in to the mailbox,   Click the verification link,   Verify the account number

Login npm account

After the NPM account registration is completed, execute it at the command prompt (git,Node.js)   npm   login command, enter the user name, password and email in sequence, and then you can log in successfully

Note: npm is running   Before the login command, switch the server under the package back to the official server of npm, otherwise it will fail and nrm can be used   ls view (first install nrm and write the command npm i   nrm -g install nrm, through nrm   ls can view the current server. If not, npm can be used   config   get   registry (view)

A standardized package must have the following points:

1. The package must exist in a separate directory

2. The package management configuration file package.json must be included in the package directory

3.package.json must contain name,   Version (version) and main (package entry) are three attributes

First, we need to prepare the package infrastructure:

Create a new folder as the root directory (Note: this name cannot be the same, so you need to go to npm in advance to check whether there is a similar name)

Then, in the root directory, create the following three files:

Package.json (package management configuration file) (package.josn can be written with the command: npm   init -y, at the command prompt, Git,Nods.js (note the path)

Index.js (package entry file)

Readme.md (description document of package)

Next, let's initialize the package.json configuration file

{
  "name": "flightloong-tools",
  "version": "1.0.0",
  "description": "Provide formatting time HTMLEscape Related functions",
  "main": "index.js",
  "keywords": [
    "itcast",
    "itheima",
    "dateFormat",
    "escape"
  ],
  "license": "ISC"
}

For reference only

If npm is used   init -y is written. These are all out. You don't need to write them manually

After initialization, you can write what you want to write in index.js. If you want to experience, please refer to this. This is the method to define the format time

// Package entry file index.js

// A function that defines the formatting time
function dateFormat (dateStr) {
  const dt = new Date(dateStr)

  const y = padZero(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}`
}

// Define a function that complements zero
function padZero (n) {
  return n > 9 ? n : '0' + n
}

// Expose required members
module.exports = {
  dateFormat
}

For reference only

Let's write the documentation of the package

README.md contains the following 6 items

Installation mode

Import mode

Format time

Escape special characters in HTML

Restore special characters in HTML

Open source protocol

### install
​```
npm i flightloong-tools
​```

### Import
​```js
const itheima = require('./flightloong-tools')
​```

### Format time
​```js
// Call dateFormat to format the time
const dtStr = itheima.dateFormat(new Date())
// Result 2020-04-03 17:20:58
console.log(dtStr)
​```

### Escape special characters in HTML
​```js
// HTML string with conversion
const htmlStr = '<h1 title="abc">This is h1 label<span>123&nbsp;</span></h1>'
// Call the htmlEscape method to convert
const str = itheima.htmlEscape(htmlStr)
// Result of conversion & lt; h1 title=" abc"& gt; This is the H1 tag & lt; span> 123& nbsp;& lt;/ span>& lt;/ h1>
console.log(str)
​```

### Restore special characters in HTML
​```js
// HTML string to restore
const str2 = itheima.htmlUnEscape(str)
// The output result < H1 title = "ABC" > this is the H1 tag < span > 123 & nbsp</ span></h1>
console.log(str2)
​```

### Open source protocol
ISC

For reference only

After writing, you can publish it

After publishing, switch the terminal to the root directory of the package and run npm   With the publish command, you can publish the package to npm

If you want to delete, you can run npm   unpublish   The name of the package -- force command, you can delete the published package from npm

be careful:

npm   The unpublish command can only delete packages published within 72 hours

npm   unpublish deleted packages are not allowed to be published repeatedly within 24 hours

Be careful when releasing packages. Try not to release meaningless packages to npm!

Posted by grevathi_02 on Thu, 07 Oct 2021 12:39:11 -0700