ipaddress 0.2.3 → 0.2.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/{crunchy.d.ts → dist/src/crunchy.d.ts} +1 -0
- package/dist/src/crunchy.d.ts.map +1 -0
- package/dist/src/crunchy.js +484 -0
- package/dist/src/index.d.ts +13 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +21 -0
- package/{ip_bits.d.ts → dist/src/ip_bits.d.ts} +6 -5
- package/dist/src/ip_bits.d.ts.map +1 -0
- package/dist/src/ip_bits.js +133 -0
- package/{ip_version.d.ts → dist/src/ip_version.d.ts} +2 -1
- package/dist/src/ip_version.d.ts.map +1 -0
- package/dist/src/ip_version.js +6 -0
- package/{ipaddress.d.ts → dist/src/ipaddress.d.ts} +18 -16
- package/dist/src/ipaddress.d.ts.map +1 -0
- package/dist/src/ipaddress.js +1228 -0
- package/dist/src/ipv4.d.ts +15 -0
- package/dist/src/ipv4.d.ts.map +1 -0
- package/dist/src/ipv4.js +176 -0
- package/dist/src/ipv6.d.ts +13 -0
- package/dist/src/ipv6.d.ts.map +1 -0
- package/dist/src/ipv6.js +190 -0
- package/dist/src/ipv6_loopback.d.ts +6 -0
- package/dist/src/ipv6_loopback.d.ts.map +1 -0
- package/dist/src/ipv6_loopback.js +51 -0
- package/dist/src/ipv6_mapped.d.ts +6 -0
- package/dist/src/ipv6_mapped.d.ts.map +1 -0
- package/dist/src/ipv6_mapped.js +159 -0
- package/dist/src/ipv6_unspec.d.ts +6 -0
- package/dist/src/ipv6_unspec.d.ts.map +1 -0
- package/dist/src/ipv6_unspec.js +57 -0
- package/{prefix.d.ts → dist/src/prefix.d.ts} +10 -7
- package/dist/src/prefix.d.ts.map +1 -0
- package/dist/src/prefix.js +142 -0
- package/dist/src/prefix128.d.ts +7 -0
- package/dist/src/prefix128.d.ts.map +1 -0
- package/dist/src/prefix128.js +35 -0
- package/dist/src/prefix32.d.ts +7 -0
- package/dist/src/prefix32.d.ts.map +1 -0
- package/dist/src/prefix32.js +25 -0
- package/dist/src/rle.d.ts +19 -0
- package/dist/src/rle.d.ts.map +1 -0
- package/dist/src/rle.js +73 -0
- package/dist/test/test_crunchy.d.ts +2 -0
- package/dist/test/test_crunchy.d.ts.map +1 -0
- package/dist/test/test_crunchy.js +451 -0
- package/dist/test/test_ipaddress.d.ts +2 -0
- package/dist/test/test_ipaddress.d.ts.map +1 -0
- package/dist/test/test_ipaddress.js +182 -0
- package/dist/test/test_ipv4.d.ts +2 -0
- package/dist/test/test_ipv4.d.ts.map +1 -0
- package/dist/test/test_ipv4.js +561 -0
- package/dist/test/test_ipv6.d.ts +2 -0
- package/dist/test/test_ipv6.d.ts.map +1 -0
- package/dist/test/test_ipv6.js +313 -0
- package/dist/test/test_ipv6_loopback.d.ts +2 -0
- package/dist/test/test_ipv6_loopback.d.ts.map +1 -0
- package/dist/test/test_ipv6_loopback.js +40 -0
- package/dist/test/test_ipv6_mapped.d.ts +2 -0
- package/dist/test/test_ipv6_mapped.d.ts.map +1 -0
- package/dist/test/test_ipv6_mapped.js +89 -0
- package/dist/test/test_ipv6_unspec.d.ts +2 -0
- package/dist/test/test_ipv6_unspec.d.ts.map +1 -0
- package/dist/test/test_ipv6_unspec.js +39 -0
- package/dist/test/test_prefix128.d.ts +2 -0
- package/dist/test/test_prefix128.d.ts.map +1 -0
- package/dist/test/test_prefix128.js +35 -0
- package/dist/test/test_prefix32.d.ts +2 -0
- package/dist/test/test_prefix32.d.ts.map +1 -0
- package/dist/test/test_prefix32.js +126 -0
- package/dist/test/test_rle.d.ts +2 -0
- package/dist/test/test_rle.d.ts.map +1 -0
- package/dist/test/test_rle.js +141 -0
- package/dist/vitest.config.d.ts +3 -0
- package/dist/vitest.config.d.ts.map +1 -0
- package/dist/vitest.config.js +11 -0
- package/package.json +40 -37
- package/src/crunchy.ts +546 -0
- package/src/index.ts +23 -0
- package/src/ip_bits.ts +148 -0
- package/src/ip_version.ts +6 -0
- package/src/ipaddress.ts +1331 -0
- package/src/ipv4.ts +190 -0
- package/src/ipv6.ts +202 -0
- package/src/ipv6_loopback.ts +55 -0
- package/src/ipv6_mapped.ts +163 -0
- package/src/ipv6_unspec.ts +59 -0
- package/src/prefix.ts +170 -0
- package/src/prefix128.ts +38 -0
- package/src/prefix32.ts +28 -0
- package/src/rle.ts +85 -0
- package/test/test_crunchy.ts +614 -0
- package/test/test_ipaddress.ts +208 -0
- package/test/test_ipv4.ts +596 -0
- package/test/test_ipv6.ts +325 -0
- package/test/test_ipv6_loopback.ts +52 -0
- package/test/test_ipv6_mapped.ts +101 -0
- package/test/test_ipv6_unspec.ts +51 -0
- package/test/test_prefix128.ts +39 -0
- package/test/test_prefix32.ts +130 -0
- package/test/test_rle.ts +144 -0
- package/LICENSE +0 -20
- package/README.md +0 -956
- package/index.d.ts +0 -12
- package/ipaddress.js +0 -2854
- package/ipaddress.js.map +0 -1
- package/ipv4.d.ts +0 -14
- package/ipv6.d.ts +0 -12
- package/ipv6_loopback.d.ts +0 -5
- package/ipv6_mapped.d.ts +0 -5
- package/ipv6_unspec.d.ts +0 -5
- package/prefix128.d.ts +0 -6
- package/prefix32.d.ts +0 -6
- package/rle.d.ts +0 -14
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import IPAddress from "./ipaddress.js";
|
|
2
|
+
export const Ipv6Unspec = {
|
|
3
|
+
// The address with all zero bits is called the +unspecified+ address
|
|
4
|
+
// (corresponding to 0.0.0.0 in IPv4). It should be something like this:
|
|
5
|
+
//
|
|
6
|
+
// 0000:0000:0000:0000:0000:0000:0000:0000
|
|
7
|
+
//
|
|
8
|
+
// but, with the use of compression, it is usually written as just two
|
|
9
|
+
// colons:
|
|
10
|
+
//
|
|
11
|
+
// ::
|
|
12
|
+
//
|
|
13
|
+
// or, specifying the netmask:
|
|
14
|
+
//
|
|
15
|
+
// ::/128
|
|
16
|
+
//
|
|
17
|
+
// With IPAddress, create a new unspecified IPv6 address using its own
|
|
18
|
+
// subclass:
|
|
19
|
+
//
|
|
20
|
+
// ip = IPAddress::IPv6::Unspecified.new
|
|
21
|
+
//
|
|
22
|
+
// ip.to_s
|
|
23
|
+
// // => => "::/128"
|
|
24
|
+
//
|
|
25
|
+
// You can easily check if an IPv6 object is an unspecified address by
|
|
26
|
+
// using the IPv6// unspecified? method
|
|
27
|
+
//
|
|
28
|
+
// ip.unspecified?
|
|
29
|
+
// // => true
|
|
30
|
+
//
|
|
31
|
+
// An unspecified IPv6 address can also be created with the wrapper
|
|
32
|
+
// method, like we've seen before
|
|
33
|
+
//
|
|
34
|
+
// ip = IPAddress "::"
|
|
35
|
+
//
|
|
36
|
+
// ip.unspecified?
|
|
37
|
+
// // => true
|
|
38
|
+
//
|
|
39
|
+
// This address must never be assigned to an interface and is to be used
|
|
40
|
+
// only in software before the application has learned its host's source
|
|
41
|
+
// address appropriate for a pending connection. Routers must not forward
|
|
42
|
+
// packets with the unspecified address.
|
|
43
|
+
//
|
|
44
|
+
//
|
|
45
|
+
// Creates a new IPv6 unspecified address
|
|
46
|
+
//
|
|
47
|
+
// ip = IPAddress::IPv6::Unspecified.new
|
|
48
|
+
//
|
|
49
|
+
// ip.to_s
|
|
50
|
+
// // => => "::/128"
|
|
51
|
+
//
|
|
52
|
+
create() {
|
|
53
|
+
return IPAddress.parse("::");
|
|
54
|
+
},
|
|
55
|
+
// class IPv6::Unspecified
|
|
56
|
+
};
|
|
57
|
+
export default Ipv6Unspec;
|
|
@@ -1,16 +1,18 @@
|
|
|
1
|
-
import IpBits from
|
|
2
|
-
import Crunchy from
|
|
3
|
-
export
|
|
4
|
-
|
|
1
|
+
import IpBits from "./ip_bits.js";
|
|
2
|
+
import Crunchy from "./crunchy.js";
|
|
3
|
+
export type From = (source: Prefix, num: number) => Prefix;
|
|
4
|
+
interface PrefixParams {
|
|
5
|
+
num: number;
|
|
6
|
+
ip_bits: IpBits;
|
|
7
|
+
net_mask: Crunchy;
|
|
8
|
+
vt_from: From;
|
|
5
9
|
}
|
|
6
10
|
export declare class Prefix {
|
|
7
11
|
num: number;
|
|
8
12
|
ip_bits: IpBits;
|
|
9
13
|
net_mask: Crunchy;
|
|
10
14
|
vt_from: From;
|
|
11
|
-
constructor(obj:
|
|
12
|
-
[id: string]: any;
|
|
13
|
-
});
|
|
15
|
+
constructor(obj: PrefixParams);
|
|
14
16
|
clone(): Prefix;
|
|
15
17
|
eq(other: Prefix): boolean;
|
|
16
18
|
ne(other: Prefix): boolean;
|
|
@@ -32,3 +34,4 @@ export declare class Prefix {
|
|
|
32
34
|
sub(other: number): Prefix;
|
|
33
35
|
}
|
|
34
36
|
export default Prefix;
|
|
37
|
+
//# sourceMappingURL=prefix.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"prefix.d.ts","sourceRoot":"","sources":["../../src/prefix.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,cAAc,CAAC;AAClC,OAAO,OAAO,MAAM,cAAc,CAAC;AAEnC,MAAM,MAAM,IAAI,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,KAAK,MAAM,CAAC;AAE3D,UAAU,YAAY;IACpB,GAAG,EAAE,MAAM,CAAC;IACZ,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,EAAE,OAAO,CAAC;IAClB,OAAO,EAAE,IAAI,CAAC;CACf;AAED,qBAAa,MAAM;IACjB,GAAG,EAAE,MAAM,CAAC;IACZ,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,EAAE,OAAO,CAAC;IAClB,OAAO,EAAE,IAAI,CAAC;gBAEF,GAAG,EAAE,YAAY;IAOtB,KAAK,IAAI,MAAM;IASf,EAAE,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO;IAI1B,EAAE,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO;IAI1B,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM;IAiBxB,IAAI,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM;IAIzB,SAAS,IAAI,MAAM;IAInB,IAAI,IAAI,OAAO;WAIR,WAAW,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO;IAUzD,OAAO,IAAI,OAAO;IAIlB,UAAU,IAAI,MAAM;IAapB,SAAS,IAAI,OAAO;IAiBpB,WAAW,IAAI,MAAM;IAcrB,IAAI,IAAI,MAAM;IAQd,IAAI,IAAI,MAAM;IAQd,IAAI,IAAI,MAAM;IAId,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM;IAIjC,GAAG,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM;IAI1B,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM;IAIjC,GAAG,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM;CAMlC;AAED,eAAe,MAAM,CAAC"}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import Crunchy from "./crunchy.js";
|
|
2
|
+
export class Prefix {
|
|
3
|
+
num;
|
|
4
|
+
ip_bits;
|
|
5
|
+
net_mask;
|
|
6
|
+
vt_from;
|
|
7
|
+
constructor(obj) {
|
|
8
|
+
this.num = obj.num;
|
|
9
|
+
this.ip_bits = obj.ip_bits;
|
|
10
|
+
this.net_mask = obj.net_mask;
|
|
11
|
+
this.vt_from = obj.vt_from;
|
|
12
|
+
}
|
|
13
|
+
clone() {
|
|
14
|
+
return new Prefix({
|
|
15
|
+
num: this.num,
|
|
16
|
+
ip_bits: this.ip_bits,
|
|
17
|
+
net_mask: this.net_mask,
|
|
18
|
+
vt_from: this.vt_from,
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
eq(other) {
|
|
22
|
+
return this.ip_bits.version == other.ip_bits.version && this.num == other.num;
|
|
23
|
+
}
|
|
24
|
+
ne(other) {
|
|
25
|
+
return !this.eq(other);
|
|
26
|
+
}
|
|
27
|
+
cmp(oth) {
|
|
28
|
+
if (this.ip_bits.version < oth.ip_bits.version) {
|
|
29
|
+
return -1;
|
|
30
|
+
}
|
|
31
|
+
else if (this.ip_bits.version > oth.ip_bits.version) {
|
|
32
|
+
return 1;
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
if (this.num < oth.num) {
|
|
36
|
+
return -1;
|
|
37
|
+
}
|
|
38
|
+
else if (this.num > oth.num) {
|
|
39
|
+
return 1;
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
return 0;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
// #[allow(dead_code)]
|
|
47
|
+
from(num) {
|
|
48
|
+
return this.vt_from(this, num);
|
|
49
|
+
}
|
|
50
|
+
to_ip_str() {
|
|
51
|
+
return this.ip_bits.vt_as_compressed_string(this.ip_bits, this.net_mask);
|
|
52
|
+
}
|
|
53
|
+
size() {
|
|
54
|
+
return Crunchy.one().shl(this.ip_bits.bits - this.num);
|
|
55
|
+
}
|
|
56
|
+
static new_netmask(prefix, bits) {
|
|
57
|
+
let mask = Crunchy.zero();
|
|
58
|
+
const host_prefix = bits - prefix;
|
|
59
|
+
for (let i = 0; i < prefix; ++i) {
|
|
60
|
+
// console.log(">>>", i, host_prefix, mask);
|
|
61
|
+
mask = mask.add(Crunchy.one().shl(host_prefix + i));
|
|
62
|
+
}
|
|
63
|
+
return mask;
|
|
64
|
+
}
|
|
65
|
+
netmask() {
|
|
66
|
+
return this.net_mask;
|
|
67
|
+
}
|
|
68
|
+
get_prefix() {
|
|
69
|
+
return this.num;
|
|
70
|
+
}
|
|
71
|
+
// The hostmask is the contrary of the subnet mask,
|
|
72
|
+
// as it shows the bits that can change within the
|
|
73
|
+
// hosts
|
|
74
|
+
//
|
|
75
|
+
// prefix = IPAddress::Prefix32.new 24
|
|
76
|
+
//
|
|
77
|
+
// prefix.hostmask
|
|
78
|
+
// // => "0.0.0.255"
|
|
79
|
+
//
|
|
80
|
+
host_mask() {
|
|
81
|
+
let ret = Crunchy.zero();
|
|
82
|
+
for (let _ = 0; _ < this.ip_bits.bits - this.num; _++) {
|
|
83
|
+
ret = ret.shl(1).add(Crunchy.one());
|
|
84
|
+
}
|
|
85
|
+
return ret;
|
|
86
|
+
}
|
|
87
|
+
//
|
|
88
|
+
// Returns the length of the host portion
|
|
89
|
+
// of a netmask.
|
|
90
|
+
//
|
|
91
|
+
// prefix = Prefix128.new 96
|
|
92
|
+
//
|
|
93
|
+
// prefix.host_prefix
|
|
94
|
+
// // => 128
|
|
95
|
+
//
|
|
96
|
+
host_prefix() {
|
|
97
|
+
return this.ip_bits.bits - this.num;
|
|
98
|
+
}
|
|
99
|
+
//
|
|
100
|
+
// Transforms the prefix into a string of bits
|
|
101
|
+
// representing the netmask
|
|
102
|
+
//
|
|
103
|
+
// prefix = IPAddress::Prefix128.new 64
|
|
104
|
+
//
|
|
105
|
+
// prefix.bits
|
|
106
|
+
// // => "1111111111111111111111111111111111111111111111111111111111111111"
|
|
107
|
+
// "0000000000000000000000000000000000000000000000000000000000000000"
|
|
108
|
+
//
|
|
109
|
+
bits() {
|
|
110
|
+
return this.netmask().toString(2);
|
|
111
|
+
}
|
|
112
|
+
// #[allow(dead_code)]
|
|
113
|
+
// public net_mask(&self) -> BigUint {
|
|
114
|
+
// return (self.in_mask.clone() >> (self.host_prefix() as usize)) << (self.host_prefix() as usize);
|
|
115
|
+
// }
|
|
116
|
+
to_s() {
|
|
117
|
+
return this.get_prefix().toString();
|
|
118
|
+
}
|
|
119
|
+
// #[allow(dead_code)]
|
|
120
|
+
// public inspect(&self) -> String {
|
|
121
|
+
// return self.to_s();
|
|
122
|
+
// }
|
|
123
|
+
to_i() {
|
|
124
|
+
return this.get_prefix();
|
|
125
|
+
}
|
|
126
|
+
add_prefix(other) {
|
|
127
|
+
return this.from(this.get_prefix() + other.get_prefix());
|
|
128
|
+
}
|
|
129
|
+
add(other) {
|
|
130
|
+
return this.from(this.get_prefix() + other);
|
|
131
|
+
}
|
|
132
|
+
sub_prefix(other) {
|
|
133
|
+
return this.sub(other.get_prefix());
|
|
134
|
+
}
|
|
135
|
+
sub(other) {
|
|
136
|
+
if (other > this.get_prefix()) {
|
|
137
|
+
return this.from(other - this.get_prefix());
|
|
138
|
+
}
|
|
139
|
+
return this.from(this.get_prefix() - other);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
export default Prefix;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"prefix128.d.ts","sourceRoot":"","sources":["../../src/prefix128.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,aAAa,CAAC;AAGjC,QAAA,MAAM,SAAS;gBAaD,MAAM,GAAG,MAAM;aAgBlB,MAAM,OAAO,MAAM,GAAG,MAAM;CAGtC,CAAC;AAEF,eAAe,SAAS,CAAC"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import Prefix from "./prefix.js";
|
|
2
|
+
import IpBits from "./ip_bits.js";
|
|
3
|
+
const Prefix128 = {
|
|
4
|
+
// #[derive(Ord,PartialOrd,Eq,PartialEq,Debug,Copy,Clone)]
|
|
5
|
+
// pub struct Prefix128 {
|
|
6
|
+
// }
|
|
7
|
+
//
|
|
8
|
+
// impl Prefix128 {
|
|
9
|
+
//
|
|
10
|
+
// Creates a new prefix object for 128 bits IPv6 addresses
|
|
11
|
+
//
|
|
12
|
+
// prefix = IPAddressPrefix128.new 64
|
|
13
|
+
// // => 64
|
|
14
|
+
//
|
|
15
|
+
// #[allow(unused_comparisons)]
|
|
16
|
+
create(num) {
|
|
17
|
+
if (num <= 128) {
|
|
18
|
+
// static _FROM: &'static (Fn(&Prefix, usize) -> Result<Prefix, String>) = &from;
|
|
19
|
+
// static _TO_IP_STR: &'static (Fn(&Vec<u16>) -> String) = &Prefix128::to_ip_str;
|
|
20
|
+
const ip_bits = IpBits.v6();
|
|
21
|
+
const bits = ip_bits.bits;
|
|
22
|
+
return new Prefix({
|
|
23
|
+
num,
|
|
24
|
+
ip_bits,
|
|
25
|
+
net_mask: Prefix.new_netmask(num, bits),
|
|
26
|
+
vt_from: Prefix128.from, // vt_to_ip_str: _TO_IP_STR
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
return null;
|
|
30
|
+
},
|
|
31
|
+
from(my, num) {
|
|
32
|
+
return Prefix128.create(num);
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
export default Prefix128;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"prefix32.d.ts","sourceRoot":"","sources":["../../src/prefix32.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,aAAa,CAAC;AAGjC,QAAA,MAAM,QAAQ;aACH,MAAM,OAAO,MAAM,GAAG,MAAM;gBAIzB,MAAM,GAAG,MAAM;CAiB5B,CAAC;AAEF,eAAe,QAAQ,CAAC"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import Prefix from "./prefix.js";
|
|
2
|
+
import IpBits from "./ip_bits.js";
|
|
3
|
+
const Prefix32 = {
|
|
4
|
+
from(my, num) {
|
|
5
|
+
return Prefix32.create(num);
|
|
6
|
+
},
|
|
7
|
+
create(num) {
|
|
8
|
+
if (num >= 0 && num <= 32) {
|
|
9
|
+
// static _FROM: &'static (Fn(&::prefix::Prefix, usize) -> Result<::prefix::Prefix, String>) =
|
|
10
|
+
// &from;
|
|
11
|
+
// static _TO_IP_STR: &'static (Fn(&Vec<u16>) -> String) = &to_ip_str;
|
|
12
|
+
const ip_bits = IpBits.v4();
|
|
13
|
+
const bits = ip_bits.bits;
|
|
14
|
+
return new Prefix({
|
|
15
|
+
num,
|
|
16
|
+
ip_bits,
|
|
17
|
+
net_mask: Prefix.new_netmask(num, bits),
|
|
18
|
+
vt_from: Prefix32.from,
|
|
19
|
+
// vt_to_ip_str: _TO_IP_STR,
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
return null;
|
|
23
|
+
},
|
|
24
|
+
};
|
|
25
|
+
export default Prefix32;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
interface RleParams {
|
|
2
|
+
part: number;
|
|
3
|
+
pos: number;
|
|
4
|
+
cnt: number;
|
|
5
|
+
max: boolean;
|
|
6
|
+
}
|
|
7
|
+
declare class Rle {
|
|
8
|
+
part: number;
|
|
9
|
+
pos: number;
|
|
10
|
+
cnt: number;
|
|
11
|
+
max: boolean;
|
|
12
|
+
constructor(obj: RleParams);
|
|
13
|
+
toString(): string;
|
|
14
|
+
eq(other: Rle): boolean;
|
|
15
|
+
ne(other: Rle): boolean;
|
|
16
|
+
static code(parts: number[]): Rle[];
|
|
17
|
+
}
|
|
18
|
+
export default Rle;
|
|
19
|
+
//# sourceMappingURL=rle.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rle.d.ts","sourceRoot":"","sources":["../../src/rle.ts"],"names":[],"mappings":"AAkCA,UAAU,SAAS;IACjB,IAAI,EAAE,MAAM,CAAC;IACb,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,EAAE,OAAO,CAAC;CACd;AAED,cAAM,GAAG;IACA,IAAI,EAAE,MAAM,CAAC;IACb,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,EAAE,OAAO,CAAC;gBAER,GAAG,EAAE,SAAS;IAOnB,QAAQ;IAIR,EAAE,CAAC,KAAK,EAAE,GAAG,GAAG,OAAO;IAIvB,EAAE,CAAC,KAAK,EAAE,GAAG,GAAG,OAAO;WAIhB,IAAI,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,GAAG,EAAE;CAgB3C;AAED,eAAe,GAAG,CAAC"}
|
package/dist/src/rle.js
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
class Last {
|
|
2
|
+
val;
|
|
3
|
+
max_poses = [];
|
|
4
|
+
ret = [];
|
|
5
|
+
handle_last() {
|
|
6
|
+
if (this.val == null) {
|
|
7
|
+
return;
|
|
8
|
+
}
|
|
9
|
+
const _last = this.val;
|
|
10
|
+
let max_rles = this.max_poses[_last.part];
|
|
11
|
+
if (max_rles == null) {
|
|
12
|
+
max_rles = this.max_poses[_last.part] = [];
|
|
13
|
+
}
|
|
14
|
+
// console.log(_last.part, this.max_poses);
|
|
15
|
+
for (const idx in max_rles) {
|
|
16
|
+
const prev = this.ret[max_rles[idx]];
|
|
17
|
+
if (prev.cnt > _last.cnt) {
|
|
18
|
+
// console.log(`>>>>> last=${_last}->${idx}->prev=${prev}`);
|
|
19
|
+
_last.max = false;
|
|
20
|
+
}
|
|
21
|
+
else if (prev.cnt == _last.cnt) {
|
|
22
|
+
// nothing
|
|
23
|
+
}
|
|
24
|
+
else if (prev.cnt < _last.cnt) {
|
|
25
|
+
// console.log(`<<<<< last=${_last}->${idx}->prev=${prev}`);
|
|
26
|
+
prev.max = false;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
// println!("push:{}:{:?}", self.ret.len(), _last);
|
|
30
|
+
max_rles.push(this.ret.length);
|
|
31
|
+
_last.pos = this.ret.length;
|
|
32
|
+
this.ret.push(_last);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
class Rle {
|
|
36
|
+
part;
|
|
37
|
+
pos;
|
|
38
|
+
cnt;
|
|
39
|
+
max;
|
|
40
|
+
constructor(obj) {
|
|
41
|
+
this.part = obj.part;
|
|
42
|
+
this.pos = obj.pos;
|
|
43
|
+
this.cnt = obj.cnt;
|
|
44
|
+
this.max = obj.max;
|
|
45
|
+
}
|
|
46
|
+
toString() {
|
|
47
|
+
return `<Rle@part:${this.part},pos:${this.pos},cnt:${this.cnt},max:${this.max}>`;
|
|
48
|
+
}
|
|
49
|
+
eq(other) {
|
|
50
|
+
return this.part == other.part && this.pos == other.pos && this.cnt == other.cnt && this.max == other.max;
|
|
51
|
+
}
|
|
52
|
+
ne(other) {
|
|
53
|
+
return !this.eq(other);
|
|
54
|
+
}
|
|
55
|
+
static code(parts) {
|
|
56
|
+
const last = new Last();
|
|
57
|
+
// println!("code");
|
|
58
|
+
for (const part of parts) {
|
|
59
|
+
// const part = parts[i]
|
|
60
|
+
// console.log(`part:${part}`);
|
|
61
|
+
if (last.val && last.val.part == part) {
|
|
62
|
+
last.val.cnt += 1;
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
last.handle_last();
|
|
66
|
+
last.val = new Rle({ part, pos: 0, cnt: 1, max: true });
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
last.handle_last();
|
|
70
|
+
return last.ret;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
export default Rle;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"test_crunchy.d.ts","sourceRoot":"","sources":["../../test/test_crunchy.ts"],"names":[],"mappings":""}
|