npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

swc-plugin-accuracy

v1.0.2

Published

swc-loader 插件 提供处理js 计算精度丢失的统一方案

Downloads

112

Readme

swc-plugin-accuracy

swc-loader 插件 提供处理js 计算精度丢失的统一方案 , 同时提供

  • async函数体添加try-catch功能
  • js === 严格等于处理方案
  • promise 默认添加catch处理
  • date参数处理

计算精度丢失

内部提供加、减、乘、除方法。 🌰 before

function a(b, c) {
    const d = 0.1 + 0.4
    if (b === c) {
        return 0
    }
}

after

const { accAdd, accCong } = require("babel-plugin-accuracy/src/calc.js");

function a(b, c) {
    const d = accAdd(0.1, 0.4);

    if (accCong(b, c)) {
        return 0;
    }
}

async函数添加try-catch

配置: addAsyncTry。默认为false。 如果async函数中已经被try-catch处理,则不会在添加。 🌰

before

async function printFile(filename) {
    let contents = await fs.readFileAsync(filename, 'utf8');
    console.log(contents);
}
async (filename) => {
    let contents = await fs.readFileAsync(filename, 'utf8');
    console.log(contents);
}

after

async function printFile(filename) {
    try {
        let contents = await fs.readFileAsync(filename, 'utf8');
        console.log(contents);
    } catch (error) {
        console.error(this, error);
    }
}

async filename => {
    try {
        let contents = await fs.readFileAsync(filename, 'utf8');
        console.log(contents);
    } catch (error) {
        console.error(this, error);
    }
};

promise最后添加catch

配置:promiseCatch 。 默认为false 如果promise调用链中调用了catch,则不会在添加 🌰 before

a.then(function (resolve) {
    resolve()
}).then(()=>{
    return 0
})

after

a.then(function(resolve) {
    resolve();
}).then(() => {
    return 0;
}).catch((err) => {
    console.error(err);
})

js === 严格等于

配置: checkChong 默认为false 🌰 before

function a(b, c) {
    const d = 0.1 + 0.4
    if (b === c) {
        return 0
    }
}

after

const { accSub, accCong } = require("babel-plugin-accuracy/src/calc.js");

function a(b, c) {
    const d = accSub(0.1, 0.4);

    if (accCong(b, c)) {
        return 0;
    }
}

date参数处理

🌰 before

new Date('1982-12-2')

after

new Date("1982-12-2".replace(/-/g, "/"))

在.swcrc中的完成配置使用

 {
    "jsc": {
      "parser": {
        "syntax": "typescript",
        "tsx": true,
        "dynamicImport": true,
        "privateMethod": true,
        "functionBind": true,
        "exportNamespaceFrom": true,
        "decorators": true,
        "classProperties": true
      },
      "transform": {
        "legacyDecorator": true,
        "decoratorMetadata": true,
      },
      "loose": true,
      "experimental": {
        "plugins": [
          [
            "swc-plugin-accuracy",
            {
              "checkChong": false,
              "addAsyncTry": true,
              "promiseCatch": true
            }
          ]
        ]
      }
    },
    "env": {
      "targets": "> 1%, last 2 versions, not ie <= 8",
      "mode": "usage",
      "coreJs": 3
    },
}