@kikuchan/hexdump 0.1.0-alpha.0 → 0.1.0-alpha.2

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/dist/hexdump.d.ts CHANGED
@@ -1,13 +1,43 @@
1
+ type Context = {
2
+ type: 'hex-value' | 'character-value';
3
+ address: number;
4
+ value: number;
5
+ } | {
6
+ type: 'address' | 'hex-value-no-data' | 'character-value-no-data';
7
+ address: number;
8
+ } | {
9
+ type: 'line-prefix' | 'address-prefix' | 'address-suffix' | 'hex-dump-prefix' | 'hex-group-prefix' | 'hex-gap' | 'hex-group-gap' | 'hex-group-suffix' | 'hex-dump-suffix' | 'character-prefix' | 'character-suffix' | 'line-suffix' | 'flush';
10
+ };
11
+ type ColorizerOperation = {
12
+ enter: string;
13
+ leave: string;
14
+ escape?: (s: string) => string;
15
+ } | null;
16
+ type Colorizer = boolean | undefined | 'simple' | 'html' | ((s: string, ctx: Context) => ColorizerOperation | undefined);
17
+ type Formatter = undefined | ((s: string, ctx: Context) => string | undefined);
1
18
  type Options = {
2
19
  addrOffset?: number;
20
+ addrLength?: number;
3
21
  printer?: false | ((s: string) => void);
22
+ formatter?: Formatter;
23
+ color?: Colorizer;
4
24
  prefix?: string;
5
25
  printChars?: boolean;
6
26
  foldSize?: number;
7
27
  };
8
- export declare function hexdump(buf: ArrayLike<number> | ArrayBuffer | DataView, options?: Options): string;
9
- export declare function hexdump(buf: ArrayLike<number> | ArrayBuffer | DataView, len: number, options?: Options): string;
28
+ interface Hexdumper {
29
+ (buf: ArrayLike<number> | ArrayBuffer | DataView, options?: Options): string;
30
+ (buf: ArrayLike<number> | ArrayBuffer | DataView, len: number, options?: Options): string;
31
+ }
32
+ interface Hexdump extends Hexdumper {
33
+ log: Hexdumper;
34
+ warn: Hexdumper;
35
+ error: Hexdumper;
36
+ string: Hexdumper;
37
+ create: (printer: (s: string) => void) => Hexdumper;
38
+ }
39
+ export declare const hexdump: Hexdump;
10
40
  declare const _default: {
11
- hexdump: typeof hexdump;
41
+ hexdump: Hexdump;
12
42
  };
13
43
  export default _default;
package/dist/hexdump.js CHANGED
@@ -1,2 +1,2 @@
1
- "use strict";function p(i,e){return Number(i).toString(16).padStart(e,"0")}export function hexdump(i,e,r){const s=ArrayBuffer.isView(i)?new Uint8Array(i.buffer,i.byteOffset,i.byteLength):new Uint8Array(i);typeof e!="number"&&(r=e,e=s.length),r={...r??{}},(e===void 0||e<0)&&(e=s.length),r.addrOffset===void 0&&(r.addrOffset=0);const x=r.printer||(r.printer!==!1?console.log:()=>{}),t=r.foldSize||16,l=r.printChars!==!1,o=r.addrOffset%t,O=(o+e+t-1)/t,c=[];let u="";const f=function(d){u+=d},m=r?.prefix||"";for(let d=0;d<O;d++){f(`${m}${p(r.addrOffset,8-m.length)}: `),r.addrOffset=r.addrOffset/t*t;for(let n=0;n<t;n++){const a=d*t+n-o;n%8==0&&f(" "),a<e?f(`${p(s[a],2)} `):f(" ")}if(l){f(" |");for(let n=0;n<t;n++){const a=d*t+n-o;a<e?f(s[a]>=32&&s[a]<127?String.fromCharCode(s[a]):"."):f(" ")}f("|")}x(u),c.push(u),u="",r.addrOffset+=t}return c.join(`
2
- `)}export default{hexdump};
1
+ "use strict";function v(r,n){return Number(r).toString(16).padStart(n,"0")}const h=r=>r,d=r=>r.replace(/[&<>]/g,n=>({"&":"&amp;","<":"&lt;",">":"&gt;"})[n]),H={simple:{address:{enter:"\x1B[38;5;238m",leave:"\x1B[m"},separator:{enter:"\x1B[38;5;238m",leave:"\x1B[m"},control:{enter:"\x1B[38;5;178m",leave:"\x1B[m"},ascii:{enter:"\x1B[m",leave:"\x1B[m"},exascii:{enter:"\x1B[38;5;209m",leave:"\x1B[m"},null:{enter:"\x1B[38;5;244m",leave:"\x1B[m"},normal:null},html:{address:{enter:'<span class="hexdump-address">',leave:"</span>",escape:d},separator:{enter:'<span class="hexdump-separator">',leave:"</span>",escape:d},control:{enter:'<span class="hexdump-control">',leave:"</span>",escape:d},ascii:{enter:'<span class="hexdump-ascii">',leave:"</span>",escape:d},exascii:{enter:'<span class="hexdump-exascii">',leave:"</span>",escape:d},null:{enter:'<span class="hexdump-null">',leave:"</span>",escape:d},normal:{enter:"",leave:"",escape:d}}},w=r=>{let n;if(!r)return h;if(r===!0&&(r="simple"),typeof r=="string"){const e=H[r],s=["address-prefix","address-suffix","character-prefix","character-suffix"];r=function(a,i){if(a.trim())return e.address&&i.type==="address"?e.address:e.separator&&s.includes(i.type)?e.separator:(i.type==="hex-value"||i.type==="character-value")&&typeof i.value=="number"?e.null!==void 0&&i.value===0?e.null:e.control!==void 0&&i.value<32?e.control:e.ascii!==void 0&&32<=i.value&&i.value<127?e.ascii:e.exascii!==void 0&&128<=i.value&&i.value<=255?e.exascii:e.normal:e.normal}}return(e,s)=>{const a=s.type==="flush"?null:r(e,s);return a!==void 0&&(n?.enter!==a?.enter||n?.leave!==a?.leave||n?.escape!==a?.escape)&&(e=(n?.leave||"")+(a?.enter||"")+(a?.escape||h)(e),n=a),e}},A=r=>(r||(r=h),(n,e)=>r(n,e)||"");function x(r){return(n,e,s)=>{const a=ArrayBuffer.isView(n)?new Uint8Array(n.buffer,n.byteOffset,n.byteLength):new Uint8Array(n);typeof e!="number"&&(s=e,e=a.length),s={...s??{}},(e===void 0||e<0)&&(e=a.length),r=s.printer||r;const i=A(s.formatter),b=w(s.color),o=s.foldSize||16,C=s.printChars!==!1;let l=s.addrOffset||0;const m=l%o,O=(m+e+o-1)/o,g=[];let c="";const t=function(f,p){c+=b(i(f,p),p)},y=s?.prefix||"",z=s?.addrLength??8-y.length;for(let f=0;f<O;f++){t("",{type:"line-prefix"}),t(y,{type:"address-prefix"}),t(v(l,z),{type:"address",address:l}),t(": ",{type:"address-suffix"}),l=l/o*o,t(" ",{type:"hex-dump-prefix"}),t("",{type:"hex-group-prefix"});for(let p=0;p<o;p++){const u=f*o+p-m;p&&p%8==0?(t("",{type:"hex-group-suffix"}),t(" ",{type:"hex-group-gap"}),t("",{type:"hex-group-prefix"})):p&&t(" ",{type:"hex-gap"}),u<e?t(v(a[u],2),{type:"hex-value",address:l+u,value:a[u]}):t(" ",{type:"hex-value-no-data",address:l+u})}if(t("",{type:"hex-group-suffix"}),t(" ",{type:"hex-dump-suffix"}),C){t(" |",{type:"character-prefix"});for(let p=0;p<o;p++){const u=f*o+p-m;u<e?t(a[u]>=32&&a[u]<127?String.fromCharCode(a[u]):".",{type:"character-value",address:l+u,value:a[u]}):t(" ",{type:"character-value-no-data",address:l+u})}t("|",{type:"character-suffix"})}t("",{type:"line-suffix"}),t("",{type:"flush"}),r?.(c),g.push(c),c="",l+=o}return g.join(`
2
+ `)}}export const hexdump=Object.assign(x(null),{create:x,log:x(console.log),warn:x(console.warn),error:x(console.error),string:x(null)});export default{hexdump};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kikuchan/hexdump",
3
- "version": "0.1.0-alpha.0",
3
+ "version": "0.1.0-alpha.2",
4
4
  "description": "hexdump",
5
5
  "type": "module",
6
6
  "main": "dist/hexdump.js",
@@ -25,7 +25,7 @@
25
25
  "license": "MIT",
26
26
  "devDependencies": {
27
27
  "@biomejs/biome": "^1.9.4",
28
- "esbuild": "^0.24.0",
29
- "typescript": "^5.7.2"
28
+ "esbuild": "^0.24.2",
29
+ "typescript": "^5.7.3"
30
30
  }
31
31
  }