@1-corp/fixed-size-numbers-ts 中文文档教程

发布于 5年前 浏览 15 项目主页 更新于 3年前

Fixed Size Numbers

在 TypeScript 中键入的 javascript 中固定大小整数的功能实现。

注意:这个库只是 BigNumber.js 上的类型化包装器

目前不支持浮点数或十进制值!!!

API

通过将字符串、数字或 BigNumber 格式的值传递给构造函数来创建一个新的有符号或无符号整数

使用它们各自的属性(_value 和 _size)访问值和大小

注意:_value 是始终是 BigNumber

或使用提供的方法执行安全数学

import { Uint8, Int8 } from 'fixed-sized-numbers-ts'
let safe72 = Uint8(72)
let safeNegative54 = Int8(-54)

console.log(safe72)
/*
{
    _value: 72,
    _size: 8,
    validateSize: [Function],
    add: [Function],
    sub: [Function],
    mul: [Function],
    div: [Function],
    _uint8: true
}
*/

console.log(safeNegative54)
/*
{
    _value: -54,
    _size: 8,
    validateSize: [Function],
    add: [Function],
    sub: [Function],
    mul: [Function],
    div: [Function],
    _isPositive: false
    _int8: true,
}
*/

​​只需将任何数字提供给新的构造函数即可升级任何数字

注意:此库冻结所有对象,因此您不能简单地更改构造类型的 _value。 这很好并且强制不变性。

import { Uint8, Uin64, Int8 } from 'fixed-sized-numbers-ts'
let safe72 = Uint8(72)

console.log(safe72)
/*
{
    _value: 72,
    _size: 8,
    validateSize: [Function],
    add: [Function],
    sub: [Function],
    mul: [Function],
    div: [Function],
    _uint8: true
}
*/

let safeLarger72 = Uint64(safe72._value)

console.log(safeLarger72)
/*
{
    _value: 72,
    _size: 64,
    validateSize: [Function],
    add: [Function],
    sub: [Function],
    mul: [Function],
    div: [Function],
    _uint64: true
}
*/

let signed72 = Int8(safeLarger72)

console.log(safeLarger72)
/*
{
    _value: 72,
    _size: 8,
    validateSize: [Function],
    add: [Function],
    sub: [Function],
    mul: [Function],
    div: [Function],
    isPositive: true,
    _int8: true
}
*/

注意:任何大于 32 位的东西都需要使用字符串构造,因为 Javascript 在 53 位之后不精确,这是在键入级别强制执行的

import { Uint64 } from 'fixed-sized-numbers-ts'
let bigSafeNumber = Uint64("18446744073709551616")

console.log(bigSafeNumber)
/*
{
    _value: "18446744073709551616",
    _size: 64,
    validateSize: [Function],
    add: [Function],
    sub: [Function],
    mul: [Function],
    div: [Function],
    _uint8: true
}
*/

为了安全数学,等式中的第一个值将是调用方法的人,第二个数字将是传递给它的数字。 即 Uint8(2).add() 是一个将 2 添加到任何其他 Uint8 值的函数。

注意:类型必须匹配任何算术运算。 在构造时生成方法的方式是它们需要与参数相同的类型。 此外,任何算术运算的结果都将与其父对象具有相同的类型。 这是因为算术运算生成了一个新数字。

import { Uint8, Uint16, Int8 } from 'fixed-sized-numbers-ts'
let one = Uint8(1)
let two = Uint8(2)
let three = Uint8(3)
let threeHunnid = Uint16(300)
let negOne = Uint8(-1)
let oneSigned = Uint8(1)

one.add(one) // will yield a Uint8 with _value of 2
two.sub(one) // will yield a Uint8 with _value of 1
two.mul(three) // will yield a Uint8 with _value of 6
two.div(one) // will yield a Uint8 with _value of 2

// All of these will yield a type error
one.add(oneSigned) // No Uint8 -> Int8
negOne.add(one) // No Int8 -> Uint8
one.add(negOne) // No Uint8 -> Int8
threeHunnid.add(one) // No Uint16 -> Uint8
threeHunnid.add(negOne) // no Uint16 -> Int8

Fixed Size Numbers

A functional implementation of fixed size integers in javascript with typing in TypeScript.

Note: this library is just a typed wrapper on BigNumber.js

Currently no Floating Points or Decimal values are supported!!!

API

Create a new signed or unsigned integer by passing a value in string, number, or BigNumber format to the constructor function

Access the value and the size with their respective attributes (_value, and _size)

Note: _value is always a BigNumber

Or preform safe math using the methods provided

import { Uint8, Int8 } from 'fixed-sized-numbers-ts'
let safe72 = Uint8(72)
let safeNegative54 = Int8(-54)

console.log(safe72)
/*
{
    _value: 72,
    _size: 8,
    validateSize: [Function],
    add: [Function],
    sub: [Function],
    mul: [Function],
    div: [Function],
    _uint8: true
}
*/

console.log(safeNegative54)
/*
{
    _value: -54,
    _size: 8,
    validateSize: [Function],
    add: [Function],
    sub: [Function],
    mul: [Function],
    div: [Function],
    _isPositive: false
    _int8: true,
}
*/

Upgrade any number simply by feeding it to a new constructor function

Note: This library freezes all objects, so you cannot simply change the _value of a constructed type. This is good and forces immutability.

import { Uint8, Uin64, Int8 } from 'fixed-sized-numbers-ts'
let safe72 = Uint8(72)

console.log(safe72)
/*
{
    _value: 72,
    _size: 8,
    validateSize: [Function],
    add: [Function],
    sub: [Function],
    mul: [Function],
    div: [Function],
    _uint8: true
}
*/

let safeLarger72 = Uint64(safe72._value)

console.log(safeLarger72)
/*
{
    _value: 72,
    _size: 64,
    validateSize: [Function],
    add: [Function],
    sub: [Function],
    mul: [Function],
    div: [Function],
    _uint64: true
}
*/

let signed72 = Int8(safeLarger72)

console.log(safeLarger72)
/*
{
    _value: 72,
    _size: 8,
    validateSize: [Function],
    add: [Function],
    sub: [Function],
    mul: [Function],
    div: [Function],
    isPositive: true,
    _int8: true
}
*/

Note: Anything larger than 32bits needs to be constructed using strings because of imprecission in Javascript after 53 bits, this is enforced at the typing level

import { Uint64 } from 'fixed-sized-numbers-ts'
let bigSafeNumber = Uint64("18446744073709551616")

console.log(bigSafeNumber)
/*
{
    _value: "18446744073709551616",
    _size: 64,
    validateSize: [Function],
    add: [Function],
    sub: [Function],
    mul: [Function],
    div: [Function],
    _uint8: true
}
*/

For safe math the first value in the equation will be the one who's method is being called, the second number will be the number passed to it. i.e. Uint8(2).add() is a function that will add 2 to any other Uint8 value.

Note: Types must match for any arithmetic operation. The way the methods are generated at construction time is such that they require the same type as an argument. Additionally, the result of any of the arithmetic operations will the same type as its parent object. This is because the arithmetic operations generate a new number.

import { Uint8, Uint16, Int8 } from 'fixed-sized-numbers-ts'
let one = Uint8(1)
let two = Uint8(2)
let three = Uint8(3)
let threeHunnid = Uint16(300)
let negOne = Uint8(-1)
let oneSigned = Uint8(1)

one.add(one) // will yield a Uint8 with _value of 2
two.sub(one) // will yield a Uint8 with _value of 1
two.mul(three) // will yield a Uint8 with _value of 6
two.div(one) // will yield a Uint8 with _value of 2

// All of these will yield a type error
one.add(oneSigned) // No Uint8 -> Int8
negOne.add(one) // No Int8 -> Uint8
one.add(negOne) // No Uint8 -> Int8
threeHunnid.add(one) // No Uint16 -> Uint8
threeHunnid.add(negOne) // no Uint16 -> Int8
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文