Hand in hand to teach you to send a bag

catalogue

Introduction to package

First, let's briefly understand why packages are needed:

Package classification:

Package specification (basic):

Package building steps:  

Preliminary preparation:

  Release package:

Delete package:

expand:

Introduction to package

First, let's briefly understand why packages are needed:

  • Because the built-in module of Node.js only provides some underlying API s, the efficiency of project development based on the built-in module is very low
  • The package is encapsulated based on the built-in module, provides a more advanced and convenient API, and greatly improves the development efficiency
  • The relationship between packages and built-in modules is similar to that between jQuery and browser built-in API s

Package classification:

  1. Project package: development dependency package and core dependency package
  2. Global package: it can be used anywhere on the computer after being downloaded. The project package can only be used in the project that needs to be downloaded

Package specification (basic):

  1. The package must exist as a separate directory
  2. The package.json package management configuration file must be included in the top-level directory of the package
  3. package.json must contain three attributes: name, version and main, which represent the package name, version number and package entry respectively

    Now enter package building!!!!

Package building steps:  

Preliminary preparation:

package.json: the configuration file of the package

index.js: entry file

Documentation: installation method, import method, format time, escape special characters in HTML, restore special characters in HTML, open source protocol (no mandatory requirements)

  Release package:

  1. visit https://www.npmjs.com/ , click the sign up button to enter the registration user interface
  2. Fill in account related information: full name, public email, username and password
  3. Click the create an account button to register
  4. Go back to the npm login command on the terminal and input the user name, password (the password is not displayed when you enter it, but it has already been entered. You must remember the password) and email

Note: do not log in to the distribution package on the mirror server. You need to switch the server to the npm server (the specific switching will be described separately in the following article). Go before publishing npm Check the official website to see if there are packages with the same name. If there are packages with the same name, you need to change the package name, otherwise the release will not succeed

Delete package:

Run the npm unpublish package name -- force command to delete the published package from npm

expand:

If the package has multiple functions, it is best to divide different modules!!!

// 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
}

//Export sub module
module.exports = {
  dateFormat
}

Split the time formatting function into SRC - > dateformat.js

// Functions that define escape HTML characters
function htmlEscape(htmlstr) {
  return htmlstr.replace(/<|>|"|&/g, match => {
    switch (match) {
      case '<':
        return '&glt;'
      case '>':
        return '&gt;'
      case '"':
        return '&quot;'
      case '&':
        return '&amp;'
    }
  })
}

// Defines a function to restore HTML characters
function htmlUnEscape(str) {
  return str.replace(/&glt;|&gt;|&quot;|&amp;/g, (match) => {
    switch (match) {
      case '&glt;':
        return '<'
      case '&gt;':
        return '>'
      case '&quot;':
        return '"'
      case '&amp;':
        return '&'
    }
  })
}

module.exports = {
  htmlEscape,
  htmlUnEscape
}

Split the function of processing HTML strings into SRC - > htmlescape.js

// This is the entry file for the package

const date = require('./src/dateFormat')
const escape = require('./src/htmlEscape')

// export
module.exports = {
  ...date,
  ...escape
}

In index.js, import two modules, and then use module.exports to share the corresponding methods

Xiaobai summarized, welcome to comment!!!

Keywords: Javascript node.js

Added by harchew on Thu, 07 Oct 2021 21:04:53 +0300