@allyhub/sdk 0.9.0 → 0.10.0

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.
Files changed (134) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/dist/node_modules/@allyhub/audio/package.json +1 -1
  3. package/dist/node_modules/@allyhub/chart/dist/chart-xml.d.ts +3 -0
  4. package/dist/node_modules/@allyhub/chart/dist/chart-xml.js +2 -2
  5. package/dist/node_modules/@allyhub/chart/dist/chart.d.ts +22 -0
  6. package/dist/node_modules/@allyhub/chart/dist/chart.js +1 -1
  7. package/dist/node_modules/@allyhub/chart/package.json +1 -1
  8. package/dist/node_modules/@allyhub/diagnostics/package.json +1 -1
  9. package/dist/node_modules/@allyhub/formats/dist/data/faces.d.ts +1 -1
  10. package/dist/node_modules/@allyhub/formats/dist/data/sheet.d.ts +87 -1
  11. package/dist/node_modules/@allyhub/formats/dist/data/sheet.js +1 -1
  12. package/dist/node_modules/@allyhub/formats/dist/data/slides.d.ts +89 -0
  13. package/dist/node_modules/@allyhub/formats/dist/data/slides.js +1 -1
  14. package/dist/node_modules/@allyhub/formats/dist/schemas.d.ts +50 -0
  15. package/dist/node_modules/@allyhub/formats/package.json +1 -1
  16. package/dist/node_modules/@allyhub/image-draw/package.json +1 -1
  17. package/dist/node_modules/@allyhub/image-measure/package.json +1 -1
  18. package/dist/node_modules/@allyhub/image-sdk/package.json +1 -1
  19. package/dist/node_modules/@allyhub/measure/package.json +1 -1
  20. package/dist/node_modules/@allyhub/ooxml/dist/geometry/index.js +1 -1
  21. package/dist/node_modules/@allyhub/ooxml/package.json +1 -1
  22. package/dist/node_modules/@allyhub/piece/dist/layout.d.ts +16 -0
  23. package/dist/node_modules/@allyhub/piece/dist/page/locate.js +1 -1
  24. package/dist/node_modules/@allyhub/piece/dist/stage.d.ts +6 -0
  25. package/dist/node_modules/@allyhub/piece/package.json +1 -1
  26. package/dist/node_modules/@allyhub/protocol/dist/account.d.ts +132 -0
  27. package/dist/node_modules/@allyhub/protocol/dist/account.js +1 -1
  28. package/dist/node_modules/@allyhub/protocol/dist/auth.d.ts +19 -1
  29. package/dist/node_modules/@allyhub/protocol/dist/auth.js +1 -1
  30. package/dist/node_modules/@allyhub/protocol/dist/browser-bridge.d.ts +337 -0
  31. package/dist/node_modules/@allyhub/protocol/dist/browser-bridge.js +2 -0
  32. package/dist/node_modules/@allyhub/protocol/dist/cloud.d.ts +153 -0
  33. package/dist/node_modules/@allyhub/protocol/dist/cloud.js +1 -1
  34. package/dist/node_modules/@allyhub/protocol/dist/editor.d.ts +4 -2
  35. package/dist/node_modules/@allyhub/protocol/dist/editor.js +1 -1
  36. package/dist/node_modules/@allyhub/protocol/dist/frame.d.ts +43 -188
  37. package/dist/node_modules/@allyhub/protocol/dist/frame.js +1 -1
  38. package/dist/node_modules/@allyhub/protocol/dist/index.d.ts +1 -0
  39. package/dist/node_modules/@allyhub/protocol/dist/index.js +1 -1
  40. package/dist/node_modules/@allyhub/protocol/dist/mail.d.ts +1 -0
  41. package/dist/node_modules/@allyhub/protocol/dist/mail.js +1 -1
  42. package/dist/node_modules/@allyhub/protocol/dist/notes.d.ts +51 -91
  43. package/dist/node_modules/@allyhub/protocol/dist/notes.js +1 -1
  44. package/dist/node_modules/@allyhub/protocol/dist/tools.d.ts +3 -3
  45. package/dist/node_modules/@allyhub/protocol/package.json +5 -1
  46. package/dist/node_modules/@allyhub/runtimes/package.json +1 -1
  47. package/dist/node_modules/@allyhub/scenery/dist/index.d.ts +2 -2
  48. package/dist/node_modules/@allyhub/scenery/dist/index.js +1 -1
  49. package/dist/node_modules/@allyhub/scenery/dist/moire.d.ts +45 -5
  50. package/dist/node_modules/@allyhub/scenery/dist/moire.js +2 -2
  51. package/dist/node_modules/@allyhub/scenery/dist/phone.d.ts +4 -3
  52. package/dist/node_modules/@allyhub/scenery/dist/phone.js +1 -1
  53. package/dist/node_modules/@allyhub/scenery/package.json +1 -1
  54. package/dist/node_modules/@allyhub/sheet/dist/calc/coerce.js +1 -1
  55. package/dist/node_modules/@allyhub/sheet/dist/calc/engine.d.ts +70 -0
  56. package/dist/node_modules/@allyhub/sheet/dist/calc/engine.js +1 -1
  57. package/dist/node_modules/@allyhub/sheet/dist/calc/functions/criteria.d.ts +6 -0
  58. package/dist/node_modules/@allyhub/sheet/dist/calc/functions/criteria.js +1 -1
  59. package/dist/node_modules/@allyhub/sheet/dist/calc/functions/date.js +1 -1
  60. package/dist/node_modules/@allyhub/sheet/dist/calc/functions/define.d.ts +3 -1
  61. package/dist/node_modules/@allyhub/sheet/dist/calc/functions/define.js +1 -1
  62. package/dist/node_modules/@allyhub/sheet/dist/calc/functions/info.js +1 -1
  63. package/dist/node_modules/@allyhub/sheet/dist/calc/functions/kernels.js +1 -1
  64. package/dist/node_modules/@allyhub/sheet/dist/calc/functions/lookup.js +1 -1
  65. package/dist/node_modules/@allyhub/sheet/dist/calc/functions/maths.js +1 -1
  66. package/dist/node_modules/@allyhub/sheet/dist/calc/functions/text.js +1 -1
  67. package/dist/node_modules/@allyhub/sheet/dist/calc/indexes.d.ts +9 -0
  68. package/dist/node_modules/@allyhub/sheet/dist/calc/indexes.js +1 -1
  69. package/dist/node_modules/@allyhub/sheet/dist/calc/types.d.ts +6 -0
  70. package/dist/node_modules/@allyhub/sheet/dist/calc/types.js +1 -1
  71. package/dist/node_modules/@allyhub/sheet/dist/chart.d.ts +9 -0
  72. package/dist/node_modules/@allyhub/sheet/dist/chart.js +1 -1
  73. package/dist/node_modules/@allyhub/sheet/dist/formula/ast.d.ts +12 -1
  74. package/dist/node_modules/@allyhub/sheet/dist/formula/move.d.ts +5 -1
  75. package/dist/node_modules/@allyhub/sheet/dist/formula/move.js +1 -1
  76. package/dist/node_modules/@allyhub/sheet/dist/formula/parse.js +1 -1
  77. package/dist/node_modules/@allyhub/sheet/dist/formula/print.js +1 -1
  78. package/dist/node_modules/@allyhub/sheet/dist/index.d.ts +2 -1
  79. package/dist/node_modules/@allyhub/sheet/dist/index.js +1 -1
  80. package/dist/node_modules/@allyhub/sheet/dist/inspect.d.ts +12 -2
  81. package/dist/node_modules/@allyhub/sheet/dist/inspect.js +1 -1
  82. package/dist/node_modules/@allyhub/sheet/dist/printed.d.ts +19 -0
  83. package/dist/node_modules/@allyhub/sheet/dist/printed.js +2 -0
  84. package/dist/node_modules/@allyhub/sheet/dist/values.d.ts +2 -0
  85. package/dist/node_modules/@allyhub/sheet/dist/values.js +1 -1
  86. package/dist/node_modules/@allyhub/sheet/dist/workbook.d.ts +2 -0
  87. package/dist/node_modules/@allyhub/sheet/dist/workbook.js +1 -1
  88. package/dist/node_modules/@allyhub/sheet/dist/write.js +15 -15
  89. package/dist/node_modules/@allyhub/sheet/dist/xlsx/charts.d.ts +6 -2
  90. package/dist/node_modules/@allyhub/sheet/dist/xlsx/charts.js +1 -1
  91. package/dist/node_modules/@allyhub/sheet/dist/xlsx/formula.js +2 -2
  92. package/dist/node_modules/@allyhub/sheet/dist/xlsx/import.d.ts +0 -1
  93. package/dist/node_modules/@allyhub/sheet/dist/xlsx/import.js +1 -1
  94. package/dist/node_modules/@allyhub/sheet/dist/xlsx/read.d.ts +3 -1
  95. package/dist/node_modules/@allyhub/sheet/dist/xlsx/read.js +2 -2
  96. package/dist/node_modules/@allyhub/sheet/dist/xlsx/sax.d.ts +7 -1
  97. package/dist/node_modules/@allyhub/sheet/dist/xlsx/sax.js +3 -1
  98. package/dist/node_modules/@allyhub/sheet/dist/xlsx/styles.d.ts +6 -0
  99. package/dist/node_modules/@allyhub/sheet/dist/xlsx/styles.js +1 -1
  100. package/dist/node_modules/@allyhub/sheet/dist/xlsx/write.d.ts +1 -1
  101. package/dist/node_modules/@allyhub/sheet/dist/xlsx/write.js +1 -1
  102. package/dist/node_modules/@allyhub/sheet/dist/xlsx/xml.d.ts +12 -1
  103. package/dist/node_modules/@allyhub/sheet/dist/xlsx/xml.js +3 -2
  104. package/dist/node_modules/@allyhub/sheet/package.json +1 -1
  105. package/dist/node_modules/@allyhub/slides-core/dist/api.d.ts +57 -7
  106. package/dist/node_modules/@allyhub/slides-core/dist/api.js +2 -2
  107. package/dist/node_modules/@allyhub/slides-core/dist/capabilities.js +1 -1
  108. package/dist/node_modules/@allyhub/slides-core/dist/enums.d.ts +24 -0
  109. package/dist/node_modules/@allyhub/slides-core/dist/enums.js +1 -1
  110. package/dist/node_modules/@allyhub/slides-core/dist/fact.d.ts +15 -0
  111. package/dist/node_modules/@allyhub/slides-core/dist/measure.js +1 -1
  112. package/dist/node_modules/@allyhub/slides-core/dist/model.d.ts +6 -2
  113. package/dist/node_modules/@allyhub/slides-core/dist/resolve.d.ts +3 -1
  114. package/dist/node_modules/@allyhub/slides-core/dist/resolve.js +1 -1
  115. package/dist/node_modules/@allyhub/slides-core/dist/sdk.d.ts +1 -1
  116. package/dist/node_modules/@allyhub/slides-core/dist/sdk.js +1 -1
  117. package/dist/node_modules/@allyhub/slides-core/package.json +1 -1
  118. package/dist/node_modules/@allyhub/video-core/dist/piece.d.ts +2 -2
  119. package/dist/node_modules/@allyhub/video-core/dist/schema.d.ts +2 -2
  120. package/dist/node_modules/@allyhub/video-core/dist/schema.js +1 -1
  121. package/dist/node_modules/@allyhub/video-core/package.json +1 -1
  122. package/dist/node_modules/@allyhub/video-sdk/dist/phone.d.ts +12 -5
  123. package/dist/node_modules/@allyhub/video-sdk/dist/phone.js +1 -1
  124. package/dist/node_modules/@allyhub/video-sdk/package.json +1 -1
  125. package/docs/README.md +2 -0
  126. package/docs/canvas.md +28 -0
  127. package/docs/defaults.md +20 -2
  128. package/docs/docs.md +8 -8
  129. package/docs/scenery.md +3 -4
  130. package/docs/sheets.md +22 -5
  131. package/docs/slides.md +50 -4
  132. package/docs/speech.md +1 -1
  133. package/docs/x.md +73 -0
  134. package/package.json +1 -1
@@ -22,19 +22,58 @@ export interface SensorOptions {
22
22
  */
23
23
  distortion?: number;
24
24
  /**
25
- * How the phone grades what it films: the part's `post` wherever a part
26
- * sets none. null for no grade.
25
+ * How the phone grades what it films, by what fills its frame: the room's
26
+ * grade, and as a screen comes to fill the frame, the grade it gives a lit
27
+ * screen (it exposes for the screen's light), eased between them frame by
28
+ * frame. On the scenery layer's picture alone, not on what is laid over
29
+ * it. null for no grade.
27
30
  */
28
- grade?: {
29
- filter: string;
30
- } | null;
31
+ grade?: SensorGrade | null;
31
32
  }
33
+ /**
34
+ * A grade: the picture's brightness through a curve, every colour moved
35
+ * with it so hues stay (a grade of luma alone), then the colours' strength,
36
+ * a turn of their hue, and a white balance.
37
+ */
38
+ export interface Grade {
39
+ /** [brightness in, brightness out] points, 0–1, from in 0 to in 1 in order, joined by straight lines. */
40
+ curve: [number, number][];
41
+ /** How strong the colours are after it (1: as they were; 0: grey). */
42
+ saturation?: number;
43
+ /** The colours' hue turned this many degrees. */
44
+ hue?: number;
45
+ /** Each channel's gain last, red, green and blue (a white balance; [1, 1, 1] none). */
46
+ balance?: [number, number, number];
47
+ }
48
+ /** How a phone grades what it films, by how much of its frame a screen fills (SensorOptions.grade). */
49
+ export interface SensorGrade {
50
+ /** What it films when no screen fills much of the frame: the room, a person. */
51
+ room: Grade;
52
+ /** A lit screen filmed close: the phone exposes for its light. */
53
+ screen: Grade;
54
+ /**
55
+ * The nearest screen's share of the frame where the room's grade starts
56
+ * to give way to the screen's, and where the screen's is whole (0–1).
57
+ */
58
+ share: [number, number];
59
+ }
60
+ /** How much of the screen's grade a frame takes (0–1): eased between share[0] and share[1] of the frame filled. */
61
+ export declare function screenGradeWeight(share: number, [from, to]: readonly [number, number]): number;
32
62
  /** A screen's physical pixels (screen({ pixels })). */
33
63
  export interface PanelOptions {
34
64
  /** Physical pixels per content pixel each way (a Retina laptop: 2). */
35
65
  density?: number;
36
66
  /** How much the dark lines between rows of pixels darken them (0–1). */
37
67
  rows?: number;
68
+ /**
69
+ * The pixel mesh a phone's camera close to a screen shows (0–1, 0: none):
70
+ * a fine grid of faint dark lines, every three of the panel's pixels,
71
+ * drawn on the screen's picture. At a video's resolution the panel's own
72
+ * pixels are too fine to resolve (the moiré above is what is left of
73
+ * them), yet phone footage of a screen close up is never clean: this is
74
+ * that texture, as phone photos of a laptop's screen show it.
75
+ */
76
+ mesh?: number;
38
77
  }
39
78
  export declare const SENSOR_DEFAULTS: {
40
79
  photosites: number;
@@ -46,6 +85,7 @@ export declare const SENSOR_DEFAULTS: {
46
85
  export declare const PANEL_DEFAULTS: {
47
86
  density: number;
48
87
  rows: number;
88
+ mesh: number;
49
89
  };
50
90
  /**
51
91
  * The depth a stripe a third of a pixel wide gives its fundamental,
@@ -1,5 +1,5 @@
1
1
  /*! AllyHub. Copyright (c) 2026 AllyHub, Inc. All rights reserved. Use is subject to LICENSE.md. */
2
- import{blurDiameter as z}from"./camera.js";import{homography as C}from"./screen.js";export const SENSOR_DEFAULTS={photosites:4032,lens:.35,moire:1,chroma:.3,distortion:-.03},PANEL_DEFAULTS={density:2,rows:.15},STRIPE_DEPTH=2*Math.sin(Math.PI/3)/(Math.PI/3)/2;const g=2*Math.PI,P=o=>Math.abs(o)<1e-6?1:Math.sin(Math.PI*o)/(Math.PI*o);function R(o,n){if(n===1){const i=Math.round(o[0]+o[1]),t=Math.round(o[0]-o[1]);return[(i+t)/2,(i-t)/2]}return[Math.round(2*o[0])/2,Math.round(2*o[1])/2]}const T={0:[0,0],1:[1,0],2:[1,1]};function D(o,n,i,t,f,s,e){return E(o,n,i,t,f,s,e).factor}function E(o,n,i,t,f,s,e){const r=s.pitch,c=[i[0]*r,i[1]*r],d=F(t,s),h=[d[0]/r,d[1]/r],u=R(c,o),p=T[o],a=[(c[0]-u[0])/r,(c[1]-u[1])/r],m=Math.hypot(a[0],a[1]);if(m>=.5)return{factor:1,depth:0,period:1/0};const w=c[0]*c[0]+c[1]*c[1],S=Math.exp(-2*Math.PI**2*s.lens**2*w),x=e/4,b=Math.exp(-2*Math.PI**2*x**2*(i[0]*i[0]+i[1]*i[1])),y=Math.abs(P(c[0])*P(c[1])),M=Math.abs(P(a[0])*P(a[1]))*(1-2*m),l=Math.min(1,s.strength*f*S*b*y*M),v=n-g*(u[0]*h[0]+u[1]*h[1])+g*(u[0]*p[0]+u[1]*p[1]);return{factor:1-l*(1-Math.cos(v))/2,depth:l,period:m>0?1/m:1/0}}function F(o,n){const{k1:i,centre:t,radius:f}=n.distortion,s=o[0]-t[0],e=o[1]-t[1],r=(s*s+e*e)/(f*f),c=1+i*r;return[t[0]+s*c,t[1]+e*c]}const I=[.299,.587,.114];export function moireAt(o,n,i){const t=o.inverse,[f,s]=i,e=(t[6]??0)*f+(t[7]??0)*s+(t[8]??1);if(e<=0)return;const r=((t[0]??0)*f+(t[1]??0)*s+(t[2]??0))/e,c=((t[3]??0)*f+(t[4]??0)*s+(t[5]??0))/e,[d,h]=o.size;if(r<0||c<0||r>d||c>h)return;const u=[((t[0]??0)-(t[6]??0)*r)/e,((t[1]??0)-(t[7]??0)*r)/e],p=[((t[3]??0)-(t[6]??0)*c)/e,((t[4]??0)-(t[7]??0)*c)/e],a=o.density,[m,w,S]=o.depth,x=m+w*r+S*c,b=n.blur?n.blur.k*Math.abs(x-n.blur.focus)/Math.max(x,n.blur.near):0,y=[a*u[0],a*u[1]],M=[a*p[0],a*p[1]],l=[1,1,1];for(const k of[0,1,2]){const A=D(k,g*(a*r-k/3),y,i,STRIPE_DEPTH,n,b),N=D(k,g*a*c,M,i,o.rows*STRIPE_DEPTH,n,b);l[k]=A*N}const v=I[0]*l[0]+I[1]*l[1]+I[2]*l[2];return[v+n.chroma*(l[0]-v),v+n.chroma*(l[1]-v),v+n.chroma*(l[2]-v)]}export function moireBand(o,n,i){const t=o.inverse,[f,s]=i,e=(t[6]??0)*f+(t[7]??0)*s+(t[8]??1);if(e<=0)return;const r=((t[0]??0)*f+(t[1]??0)*s+(t[2]??0))/e,c=((t[3]??0)*f+(t[4]??0)*s+(t[5]??0))/e,[d,h]=o.size;if(r<0||c<0||r>d||c>h)return;const u=[((t[0]??0)-(t[6]??0)*r)/e,((t[1]??0)-(t[7]??0)*r)/e],p=[((t[3]??0)-(t[6]??0)*c)/e,((t[4]??0)-(t[7]??0)*c)/e],a=o.density,[m,w,S]=o.depth,x=m+w*r+S*c,b=n.blur?n.blur.k*Math.abs(x-n.blur.focus)/Math.max(x,n.blur.near):0;let y={depth:0,period:1/0};for(const M of[0,1,2])for(const l of[E(M,g*(a*r-M/3),[a*u[0],a*u[1]],i,STRIPE_DEPTH,n,b),E(M,g*a*c,[a*p[0],a*p[1]],i,o.rows*STRIPE_DEPTH,n,b)])l.depth>y.depth&&(y={depth:l.depth,period:l.period});return y}export const MOIRE_SCREENS=4,MOIRE_GLSL=`#version 300 es
2
+ import{blurDiameter as $}from"./camera.js";import{homography as z}from"./screen.js";export function screenGradeWeight(o,[e,s]){if(o<=e)return 0;if(o>=s)return 1;const t=(o-e)/(s-e);return t*t*(3-2*t)}function I(o,e){if(!o)return[`${e} is missing.`];const s=[],t=o.curve,u=n=>Number.isFinite(n)&&n>=0&&n<=1;(!Array.isArray(t)||t.length<2||t.some(n=>!u(n[0])||!u(n[1]))||t.some((n,i)=>i>0&&n[0]<=(t[i-1]?.[0]??-1))||t[0]?.[0]!==0||t[t.length-1]?.[0]!==1)&&s.push(`${e}.curve is [in, out] points from 0 to 1, in order, from in 0 to in 1.`),o.saturation!==void 0&&!(Number.isFinite(o.saturation)&&o.saturation>=0)&&s.push(`${e}.saturation is 0 or more, not ${String(o.saturation)}.`);const r=o.balance;return r!==void 0&&!(Array.isArray(r)&&r.length===3&&r.every(n=>Number.isFinite(n)&&n>=0&&n<=2))&&s.push(`${e}.balance is three gains from 0 to 2, red, green and blue.`),o.hue!==void 0&&!Number.isFinite(o.hue)&&s.push(`${e}.hue is a number of degrees, not ${String(o.hue)}.`),s}export const SENSOR_DEFAULTS={photosites:4032,lens:.35,moire:1,chroma:.3,distortion:-.03},PANEL_DEFAULTS={density:2,rows:.15,mesh:.09},STRIPE_DEPTH=2*Math.sin(Math.PI/3)/(Math.PI/3)/2;const M=2*Math.PI,P=o=>Math.abs(o)<1e-6?1:Math.sin(Math.PI*o)/(Math.PI*o);function C(o,e){if(e===1){const s=Math.round(o[0]+o[1]),t=Math.round(o[0]-o[1]);return[(s+t)/2,(s-t)/2]}return[Math.round(2*o[0])/2,Math.round(2*o[1])/2]}const R={0:[0,0],1:[1,0],2:[1,1]};function A(o,e,s,t,u,r,n){return N(o,e,s,t,u,r,n).factor}function N(o,e,s,t,u,r,n){const i=r.pitch,c=[s[0]*i,s[1]*i],d=T(t,r),l=[d[0]/i,d[1]/i],f=C(c,o),p=R[o],a=[(c[0]-f[0])/i,(c[1]-f[1])/i],m=Math.hypot(a[0],a[1]);if(m>=.5)return{factor:1,depth:0,period:1/0};const S=c[0]*c[0]+c[1]*c[1],k=Math.exp(-2*Math.PI**2*r.lens**2*S),v=n/4,x=Math.exp(-2*Math.PI**2*v**2*(s[0]*s[0]+s[1]*s[1])),y=Math.abs(P(c[0])*P(c[1])),g=Math.abs(P(a[0])*P(a[1]))*(1-2*m),h=Math.min(1,r.strength*u*k*x*y*g),b=e-M*(f[0]*l[0]+f[1]*l[1])+M*(f[0]*p[0]+f[1]*p[1]);return{factor:1-h*(1-Math.cos(b))/2,depth:h,period:m>0?1/m:1/0}}function T(o,e){const{k1:s,centre:t,radius:u}=e.distortion,r=o[0]-t[0],n=o[1]-t[1],i=(r*r+n*n)/(u*u),c=1+s*i;return[t[0]+r*c,t[1]+n*c]}const E=[.299,.587,.114];export function moireAt(o,e,s){const t=o.inverse,[u,r]=s,n=(t[6]??0)*u+(t[7]??0)*r+(t[8]??1);if(n<=0)return;const i=((t[0]??0)*u+(t[1]??0)*r+(t[2]??0))/n,c=((t[3]??0)*u+(t[4]??0)*r+(t[5]??0))/n,[d,l]=o.size;if(i<0||c<0||i>d||c>l)return;const f=[((t[0]??0)-(t[6]??0)*i)/n,((t[1]??0)-(t[7]??0)*i)/n],p=[((t[3]??0)-(t[6]??0)*c)/n,((t[4]??0)-(t[7]??0)*c)/n],a=o.density,[m,S,k]=o.depth,v=m+S*i+k*c,x=e.blur?e.blur.k*Math.abs(v-e.blur.focus)/Math.max(v,e.blur.near):0,y=[a*f[0],a*f[1]],g=[a*p[0],a*p[1]],h=[1,1,1];for(const w of[0,1,2]){const D=A(w,M*(a*i-w/3),y,s,STRIPE_DEPTH,e,x),F=A(w,M*a*c,g,s,o.rows*STRIPE_DEPTH,e,x);h[w]=D*F}const b=E[0]*h[0]+E[1]*h[1]+E[2]*h[2];return[b+e.chroma*(h[0]-b),b+e.chroma*(h[1]-b),b+e.chroma*(h[2]-b)]}export function moireBand(o,e,s){const t=o.inverse,[u,r]=s,n=(t[6]??0)*u+(t[7]??0)*r+(t[8]??1);if(n<=0)return;const i=((t[0]??0)*u+(t[1]??0)*r+(t[2]??0))/n,c=((t[3]??0)*u+(t[4]??0)*r+(t[5]??0))/n,[d,l]=o.size;if(i<0||c<0||i>d||c>l)return;const f=[((t[0]??0)-(t[6]??0)*i)/n,((t[1]??0)-(t[7]??0)*i)/n],p=[((t[3]??0)-(t[6]??0)*c)/n,((t[4]??0)-(t[7]??0)*c)/n],a=o.density,[m,S,k]=o.depth,v=m+S*i+k*c,x=e.blur?e.blur.k*Math.abs(v-e.blur.focus)/Math.max(v,e.blur.near):0;let y={depth:0,period:1/0};for(const g of[0,1,2])for(const h of[N(g,M*(a*i-g/3),[a*f[0],a*f[1]],s,STRIPE_DEPTH,e,x),N(g,M*a*c,[a*p[0],a*p[1]],s,o.rows*STRIPE_DEPTH,e,x)])h.depth>y.depth&&(y={depth:h.depth,period:h.period});return y}export const MOIRE_SCREENS=4,MOIRE_GLSL=`#version 300 es
3
3
  precision highp float;
4
4
  #define SCREENS ${String(MOIRE_SCREENS)}
5
5
  uniform int count;
@@ -90,4 +90,4 @@ void main() {
90
90
  }
91
91
  // Off every screen: nothing (premultiplied).
92
92
  colour = on ? vec4(factor, 1.0) : vec4(0.0);
93
- }`;export function invert3(o){const[n=0,i=0,t=0,f=0,s=0,e=0,r=0,c=0,d=1]=o,h=s*d-e*c,u=-(f*d-e*r),p=f*c-s*r,a=n*h+i*u+t*p;if(!(Math.abs(a)<1e-18))return[h/a,-(i*d-t*c)/a,(i*e-t*s)/a,u/a,(n*d-t*r)/a,-(n*e-t*f)/a,p/a,-(n*c-i*r)/a,(n*s-i*f)/a]}export function moireScreen(o){const[n,i]=o.size,t=C(n,i,o.quad),f=t?invert3(t):void 0;if(!f)return;const s=o.camera,e=u=>(u[0]-s.position[0])*s.forward[0]+(u[1]-s.position[1])*s.forward[1]+(u[2]-s.position[2])*s.forward[2],[r,c,,d]=o.corners,h=e(r);return{inverse:f,size:[n,i],density:o.panel?.density??PANEL_DEFAULTS.density,rows:o.panel?.rows??PANEL_DEFAULTS.rows,depth:[h,(e(c)-h)/n,(e(d)-h)/i]}}export function moireCamera(o,n,i,t={},f=[0,0]){const s=t.photosites??SENSOR_DEFAULTS.photosites,e={pitch:Math.max(n[0],n[1])/s,lens:t.lens??SENSOR_DEFAULTS.lens,strength:t.moire??SENSOR_DEFAULTS.moire,chroma:t.chroma??SENSOR_DEFAULTS.chroma,distortion:{k1:t.distortion??SENSOR_DEFAULTS.distortion,centre:[n[0]/2-f[0],n[1]/2-f[1]],radius:Math.hypot(n[0],n[1])/2}},r=o.dof;if(r){const c=2*r.focus,d=z(o,c,i)*Math.max(c,o.near)/Math.abs(c-r.focus);e.blur={k:d,focus:r.focus,near:o.near}}return e}export function moireProblems(o,n){const i=[],t=(s,e)=>{s!==void 0&&!(Number.isFinite(s)&&s>0)&&i.push(`${e} is a positive number, not ${String(s)}.`)};t(o?.photosites,"sensor.photosites"),t(o?.lens,"sensor.lens"),t(n?.density,"pixels.density");const f=(s,e,r)=>{s!==void 0&&!(Number.isFinite(s)&&s>=0&&s<=r)&&i.push(`${e} is from 0 to ${String(r)}, not ${String(s)}.`)};return f(o?.moire,"sensor.moire",4),f(o?.chroma,"sensor.chroma",1),o?.distortion!==void 0&&!(Number.isFinite(o.distortion)&&Math.abs(o.distortion)<=.5)&&i.push(`sensor.distortion is from -0.5 to 0.5, not ${String(o.distortion)}.`),f(n?.rows,"pixels.rows",1),i}
93
+ }`;export function invert3(o){const[e=0,s=0,t=0,u=0,r=0,n=0,i=0,c=0,d=1]=o,l=r*d-n*c,f=-(u*d-n*i),p=u*c-r*i,a=e*l+s*f+t*p;if(!(Math.abs(a)<1e-18))return[l/a,-(s*d-t*c)/a,(s*n-t*r)/a,f/a,(e*d-t*i)/a,-(e*n-t*u)/a,p/a,-(e*c-s*i)/a,(e*r-s*u)/a]}export function moireScreen(o){const[e,s]=o.size,t=z(e,s,o.quad),u=t?invert3(t):void 0;if(!u)return;const r=o.camera,n=f=>(f[0]-r.position[0])*r.forward[0]+(f[1]-r.position[1])*r.forward[1]+(f[2]-r.position[2])*r.forward[2],[i,c,,d]=o.corners,l=n(i);return{inverse:u,size:[e,s],density:o.panel?.density??PANEL_DEFAULTS.density,rows:o.panel?.rows??PANEL_DEFAULTS.rows,depth:[l,(n(c)-l)/e,(n(d)-l)/s]}}export function moireCamera(o,e,s,t={},u=[0,0]){const r=t.photosites??SENSOR_DEFAULTS.photosites,n={pitch:Math.max(e[0],e[1])/r,lens:t.lens??SENSOR_DEFAULTS.lens,strength:t.moire??SENSOR_DEFAULTS.moire,chroma:t.chroma??SENSOR_DEFAULTS.chroma,distortion:{k1:t.distortion??SENSOR_DEFAULTS.distortion,centre:[e[0]/2-u[0],e[1]/2-u[1]],radius:Math.hypot(e[0],e[1])/2}},i=o.dof;if(i){const c=2*i.focus,d=$(o,c,s)*Math.max(c,o.near)/Math.abs(c-i.focus);n.blur={k:d,focus:i.focus,near:o.near}}return n}export function moireProblems(o,e){const s=[],t=(n,i)=>{n!==void 0&&!(Number.isFinite(n)&&n>0)&&s.push(`${i} is a positive number, not ${String(n)}.`)};t(o?.photosites,"sensor.photosites"),t(o?.lens,"sensor.lens"),t(e?.density,"pixels.density");const u=(n,i,c)=>{n!==void 0&&!(Number.isFinite(n)&&n>=0&&n<=c)&&s.push(`${i} is from 0 to ${String(c)}, not ${String(n)}.`)};u(o?.moire,"sensor.moire",4),u(o?.chroma,"sensor.chroma",1),o?.distortion!==void 0&&!(Number.isFinite(o.distortion)&&Math.abs(o.distortion)<=.5)&&s.push(`sensor.distortion is from -0.5 to 0.5, not ${String(o.distortion)}.`),u(e?.rows,"pixels.rows",1),u(e?.mesh,"pixels.mesh",1);const r=o?.grade;if(r){s.push(...I(r.room,"sensor.grade.room")),s.push(...I(r.screen,"sensor.grade.screen"));const[n=Number.NaN,i=Number.NaN]=r.share;Number.isFinite(n)&&Number.isFinite(i)&&n>=0&&n<i&&i<=1||s.push("sensor.grade.share is [from, to], 0 ≤ from < to ≤ 1.")}return s}
@@ -19,9 +19,10 @@ export interface PhoneCamera {
19
19
  * hand: a 26 mm lens on its 4:3 frame at f/8 equivalent (f/1.8 on its
20
20
  * small sensor), a 12 MP sensor, a hand whose tremor its stabiliser mostly
21
21
  * takes out, focus that hunts after a cut, a reframe or a jolt, exposure
22
- * that follows a new framing late, and a grade that takes a render's flat,
23
- * pale, cool picture to the contrast and colour of a phone filming a screen:
24
- * a screen's white stays near white, and what is dark goes darker.
22
+ * that follows a new framing late, and a grade by what fills the frame:
23
+ * a phone's gentle contrast and colour on a room, and as a screen comes to
24
+ * fill the frame, the phone exposing for its light (its white a neutral
25
+ * light grey, its middle tones much darker, its colours deep).
25
26
  */
26
27
  export declare const PHONE_CAMERA: PhoneCamera;
27
28
  /** Something with every field optional, all the way down. */
@@ -1,2 +1,2 @@
1
1
  /*! AllyHub. Copyright (c) 2026 AllyHub, Inc. All rights reserved. Use is subject to LICENSE.md. */
2
- export const PHONE_CAMERA={lens:{lens:26,lensFrame:"phone",aperture:8},sensor:{photosites:4032,lens:.35,moire:1,chroma:.3,distortion:-.03,grade:{filter:"curves=all='0/0 0.2/0.07 0.5/0.38 0.78/0.82 1/1',eq=saturation=1.5,colortemperature=temperature=6000"}},hand:{angle:.25,roll:.6,shift:.001,sway:.65,tremor:{angle:.1,frequency:8.5},drift:{angle:.15,shift:.001,period:8},stabilizer:{strength:.7,cutoff:1.5},autofocus:{},exposure:{}}};const s=e=>typeof e=="object"&&e!==null&&!Array.isArray(e);function i(e,r){if(!s(e)||!s(r))return r===void 0?e:r;const n={...e};for(const[t,o]of Object.entries(r))n[t]=o===void 0?e[t]:i(e[t],o);return n}export function phoneCamera(e={}){return i(PHONE_CAMERA,e)}
2
+ export const PHONE_CAMERA={lens:{lens:26,lensFrame:"phone",aperture:8},sensor:{photosites:4032,lens:.35,moire:1,chroma:.3,distortion:-.03,grade:{room:{curve:[[0,0],[.25,.21],[.5,.47],[.75,.78],[1,1]],saturation:1.08},screen:{curve:[[0,0],[.36,.1],[.51,.16],[.67,.46],[.8,.84],[1,1]],saturation:1.25,hue:5,balance:[1,.97,1]},share:[.5,.8]}},hand:{angle:.25,roll:.6,shift:.001,sway:.65,tremor:{angle:.1,frequency:8.5},drift:{angle:.15,shift:.001,period:8},stabilizer:{strength:.7,cutoff:1.5},autofocus:{},exposure:{}}};const s=e=>typeof e=="object"&&e!==null&&!Array.isArray(e);function i(e,r){if(!s(e)||!s(r))return r===void 0?e:r;const t={...e};for(const[n,o]of Object.entries(r))t[n]=o===void 0?e[n]:i(e[n],o);return t}export function phoneCamera(e={}){return i(PHONE_CAMERA,e)}
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@allyhub/scenery",
3
- "version": "0.9.0",
3
+ "version": "0.10.0",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {
@@ -1,2 +1,2 @@
1
1
  /*! AllyHub. Copyright (c) 2026 AllyHub, Inc. All rights reserved. Use is subject to LICENSE.md. */
2
- import{parseValue as d}from"numfmt";import{CellError as f,ERR as s,Matrix as x,numberText as h,readTyped as M}from"../values.js";export function numberOfText(e){if(!/\d/u.test(e))return;const r=M(e.trim());if(typeof r.value=="number")return r.value;if(e.trim()==="")return;const t=d(e);return typeof t?.value=="number"?t.value:void 0}export function toNumber(e){return typeof e=="number"?e:e==null?0:typeof e=="boolean"?e?1:0:e instanceof f?e:numberOfText(e)??s.VALUE}export function toText(e){return typeof e=="string"?e:e==null?"":typeof e=="number"?h(e):typeof e=="boolean"?e?"TRUE":"FALSE":e}export function toBool(e){if(typeof e=="boolean")return e;if(e==null)return!1;if(typeof e=="number")return e!==0;if(e instanceof f)return e;const r=e.trim().toUpperCase();return r==="TRUE"?!0:r==="FALSE"?!1:s.VALUE}const m=e=>typeof e=="number"?0:typeof e=="string"?1:2;export const sameNumber=(e,r)=>e===r?!0:Math.abs(e-r)>1e-13*Math.max(Math.abs(e),Math.abs(r))?!1:Number(e.toPrecision(15))===Number(r.toPrecision(15));export function compare(e,r){const t=e??(typeof r=="string"?"":typeof r=="boolean"?!1:0),n=r??(typeof e=="string"?"":typeof e=="boolean"?!1:0),o=m(t),c=m(n);if(o!==c)return o<c?-1:1;if(typeof t=="number"&&typeof n=="number")return sameNumber(t,n)?0:t<n?-1:1;if(typeof t=="string"&&typeof n=="string"){const a=t.toUpperCase(),u=n.toUpperCase();return a===u?0:a<u?-1:1}return t===n?0:t===!1?-1:1}export const finite=e=>Number.isFinite(e)?e:s.NUM;export function snap(e,r,t){return Math.abs(e)<=Math.max(Math.abs(r),Math.abs(t))*89e-17?0:e}export function arithmetic(e,r,t){const n=toNumber(r);if(n instanceof f)return n;const o=toNumber(t);if(o instanceof f)return o;switch(e){case"+":return finite(snap(n+o,n,o));case"-":return finite(snap(n-o,n,o));case"*":return finite(n*o);case"/":return o===0?s.DIV0:finite(n/o);default:return n===0&&o===0?s.NUM:n===0&&o<0?s.DIV0:finite(n**o)}}export function binary(e,r,t){if(r instanceof f)return r;if(t instanceof f)return t;switch(e){case"&":{const n=toText(r);if(n instanceof f)return n;const o=toText(t);return o instanceof f?o:`${n}${o}`}case"=":return compare(r,t)===0;case"<>":return compare(r,t)!==0;case"<":return compare(r,t)<0;case"<=":return compare(r,t)<=0;case">":return compare(r,t)>0;case">=":return compare(r,t)>=0;default:return arithmetic(e,r,t)}}export function mapBoth(e,r,t){const n=Math.max(e.rows,r.rows),o=Math.max(e.cols,r.cols),c=new Array(n*o),a=(u,i,l)=>{const p=u.rows===1?0:i,y=u.cols===1?0:l;return p<u.rows&&y<u.cols?u.at(p,y):void 0};for(let u=0;u<n;u++)for(let i=0;i<o;i++){const l=a(e,u,i),p=a(r,u,i);c[u*o+i]=l===void 0||p===void 0?s.NA:t(l,p)}return new x(n,o,c)}
2
+ import{parseValue as x}from"numfmt";import{CellError as f,ERR as i,Matrix as h,numberText as M,readTyped as b}from"../values.js";const l=new Map,w=5e4;export function numberOfText(e){if(!/\d/u.test(e))return;if(l.has(e))return l.get(e);l.size>=w&&l.clear();const r=E(e);return l.set(e,r),r}function E(e){const r=b(e.trim());if(typeof r.value=="number")return r.value;if(e.trim()==="")return;const t=x(e);return typeof t?.value=="number"?t.value:void 0}export function toNumber(e){return typeof e=="number"?e:e==null?0:typeof e=="boolean"?e?1:0:e instanceof f?e:numberOfText(e)??i.VALUE}export function toText(e){return typeof e=="string"?e:e==null?"":typeof e=="number"?M(e):typeof e=="boolean"?e?"TRUE":"FALSE":e}export function toBool(e){if(typeof e=="boolean")return e;if(e==null)return!1;if(typeof e=="number")return e!==0;if(e instanceof f)return e;const r=e.trim().toUpperCase();return r==="TRUE"?!0:r==="FALSE"?!1:i.VALUE}const y=e=>typeof e=="number"?0:typeof e=="string"?1:2;export const sameNumber=(e,r)=>e===r?!0:Math.abs(e-r)>1e-13*Math.max(Math.abs(e),Math.abs(r))?!1:Number(e.toPrecision(15))===Number(r.toPrecision(15));export function compare(e,r){const t=e??(typeof r=="string"?"":typeof r=="boolean"?!1:0),n=r??(typeof e=="string"?"":typeof e=="boolean"?!1:0),o=y(t),c=y(n);if(o!==c)return o<c?-1:1;if(typeof t=="number"&&typeof n=="number")return sameNumber(t,n)?0:t<n?-1:1;if(typeof t=="string"&&typeof n=="string"){const a=t.toUpperCase(),u=n.toUpperCase();return a===u?0:a<u?-1:1}return t===n?0:t===!1?-1:1}export const finite=e=>Number.isFinite(e)?e:i.NUM;export function snap(e,r,t){return Math.abs(e)<=Math.max(Math.abs(r),Math.abs(t))*89e-17?0:e}export function arithmetic(e,r,t){const n=toNumber(r);if(n instanceof f)return n;const o=toNumber(t);if(o instanceof f)return o;switch(e){case"+":return finite(snap(n+o,n,o));case"-":return finite(snap(n-o,n,o));case"*":return finite(n*o);case"/":return o===0?i.DIV0:finite(n/o);default:return n===0&&o===0?i.NUM:n===0&&o<0?i.DIV0:finite(n**o)}}export function binary(e,r,t){if(r instanceof f)return r;if(t instanceof f)return t;switch(e){case"&":{const n=toText(r);if(n instanceof f)return n;const o=toText(t);return o instanceof f?o:`${n}${o}`}case"=":return compare(r,t)===0;case"<>":return compare(r,t)!==0;case"<":return compare(r,t)<0;case"<=":return compare(r,t)<=0;case">":return compare(r,t)>0;case">=":return compare(r,t)>=0;default:return arithmetic(e,r,t)}}export function mapBoth(e,r,t){const n=Math.max(e.rows,r.rows),o=Math.max(e.cols,r.cols),c=new Array(n*o),a=(u,s,d)=>{const p=u.rows===1?0:s,m=u.cols===1?0:d;return p<u.rows&&m<u.cols?u.at(p,m):void 0};for(let u=0;u<n;u++)for(let s=0;s<o;s++){const d=a(e,u,s),p=a(r,u,s);c[u*o+s]=d===void 0||p===void 0?i.NA:t(d,p)}return new h(n,o,c)}
@@ -28,6 +28,20 @@ export interface CalcOptions {
28
28
  * not change from one run to the next: mulberry32 over the seed's FNV-1a hash.
29
29
  */
30
30
  export declare function seededRandom(seed: string): () => number;
31
+ /** Excel's own limits on iterative calculation, where a file's `iteration` names none. */
32
+ export declare const ITERATION_DEFAULTS: {
33
+ readonly max_iterations: 100;
34
+ readonly max_change: 0.001;
35
+ };
36
+ /** What the last iterative calculation did. */
37
+ export interface Iterated {
38
+ /** Formula cells computed each round: those around a circular reference and what reads them. */
39
+ cells: number;
40
+ /** Rounds computed, the one that found the circular reference first. */
41
+ iterations: number;
42
+ /** The most a value moved in the last round; Infinity where one changed kind (a number to an error). */
43
+ change: number;
44
+ }
31
45
  /** A cell put in the workbook. */
32
46
  export interface CellChange {
33
47
  sheet: Worksheet;
@@ -40,11 +54,25 @@ export declare class Calc {
40
54
  cycles: FormulaCell[][];
41
55
  /** How many formula cells the last pass computed. */
42
56
  computed: number;
57
+ /** The last iterative calculation, while the circular reference it worked out is there. */
58
+ iterated: Iterated | undefined;
43
59
  readonly nowSerial: () => number;
44
60
  readonly random: () => number;
45
61
  private pass;
62
+ /** The slots LAMBDA calls under way take (LAMBDA_STACK). */
63
+ private lambdaSlots;
46
64
  private readonly stack;
47
65
  private readonly inCycle;
66
+ /** Cells around a circular reference a pass found, where the workbook iterates. */
67
+ private readonly looping;
68
+ /** The data tables' cells, by their formula; made when first asked. */
69
+ private tablesOf;
70
+ /** Working data tables out: what that computes does not do it again. */
71
+ private inTables;
72
+ /** The cells the last iterative calculation computed. */
73
+ private loop;
74
+ /** The cells around circular references iterative calculation worked out since the workbook was calculated. */
75
+ private readonly around;
48
76
  private readonly namesAt;
49
77
  /** Who reads whom: made the first time something changes. */
50
78
  private readonly graph;
@@ -77,6 +105,8 @@ export declare class Calc {
77
105
  filtered(sheet: Worksheet): ReadonlySet<number>;
78
106
  /** Computes every formula. */
79
107
  calculate(): this;
108
+ /** The workbook's iterative calculation's limits, when it iterates. */
109
+ private iteration;
80
110
  /** What filters hide, from the rows computed; what leaves those out is computed again. */
81
111
  private applyFilters;
82
112
  /** The value at a place. */
@@ -88,6 +118,40 @@ export declare class Calc {
88
118
  /** What was worked out over ranges that hold changed cells is worked out again. */
89
119
  private forget;
90
120
  private run;
121
+ /** Every data table's cells, by the formula they share. */
122
+ private tableCells;
123
+ /**
124
+ * Excel's data tables, once what they read is computed: each cell what the
125
+ * table's formula gives with its inputs set to the values of the cell's
126
+ * row and column, as Excel's what-if analysis works one out. Its inputs are
127
+ * set, the model computes what that touches (iterating where it does), and
128
+ * they are put back. A two-way table's formula is the cell above its left
129
+ * column; a one-way table's are its row above (a column input) or its
130
+ * column to the left (a row input).
131
+ */
132
+ private tables;
133
+ /**
134
+ * LibreOffice's form of a data table, which it writes into the files it
135
+ * saves: TABLE(formula, input, its value[, input, its value]), as its
136
+ * MULTIPLE.OPERATIONS, each cell's references its own. Each cell is the
137
+ * formula's value with each input set to what the cell after it holds.
138
+ */
139
+ private operations;
140
+ private table;
141
+ /**
142
+ * Excel's iterative calculation: the cells around a circular reference and
143
+ * what reads them computed again, round after round, each reading the
144
+ * latest value of every other, until none moves by the maximum change or
145
+ * the rounds run out. The round that found the circular reference is the
146
+ * first.
147
+ */
148
+ private iterate;
149
+ /** Whether iterative calculation worked a cell out: its value is as close as the maximum change says. */
150
+ iterates(cell: FormulaCell): boolean;
151
+ /** The cells around circular references iterative calculation worked out: those whose start matters. */
152
+ circularCells(): readonly FormulaCell[];
153
+ /** Cells and what reads them, however far. */
154
+ private downstream;
91
155
  /**
92
156
  * What reads a spill's cells computed again until nothing moves: who reads
93
157
  * a place does not say who spills into it, so a spill that came, went or
@@ -109,6 +173,12 @@ export declare class Calc {
109
173
  private order;
110
174
  /** The formula cells yet to compute that a cell reads. */
111
175
  private pending;
176
+ /**
177
+ * A cell computed: inside another's computing, on the stack, until it is
178
+ * STACKED deep; outermost, from a list, each cell a read had to wait for
179
+ * first, then what read it again from the start.
180
+ */
181
+ private computeNow;
112
182
  private compute;
113
183
  /** What a formula computed, in its cell and those it spills into. */
114
184
  private settleResult;
@@ -1,2 +1,2 @@
1
1
  /*! AllyHub. Copyright (c) 2026 AllyHub, Inc. All rights reserved. Use is subject to LICENSE.md. */
2
- import{MAX_COLUMNS as E,MAX_ROWS as k,overlaps as O}from"../address.js";import{CellError as m,ERR as h,Matrix as p,serialOfDate as _}from"../values.js";import{filteredRows as B}from"../filter.js";import{FormulaCell as y,SharedFormula as I}from"../workbook.js";import{analyse as z}from"./analyse.js";import{Graph as T}from"./graph.js";import{fromWire as G,matrixFromWire as D,matrixToWire as W,toWire as P}from"./remote.js";import{RangeCache as U}from"./indexes.js";import{binary as S,mapBoth as H,toNumber as q}from"./coerce.js";import{FUNCTIONS as v}from"./functions/index.js";import{fillsGroup as j,paramAt as X}from"./functions/define.js";import{Lambda as M,MISSING as F,Missing as x,Reference as u,Union as b}from"./types.js";export function seededRandom(l){let t=2166136261;for(let e=0;e<l.length;e++)t=Math.imul(t^l.charCodeAt(e),16777619);return()=>{t=t+1831565813|0;let e=Math.imul(t^t>>>15,1|t);return e=e+Math.imul(e^e>>>7,61|e)^e,((e^e>>>14)>>>0)/4294967296}}const Y=new Set,Z=/\b(?:SUBTOTAL|AGGREGATE)\(/iu,$=2e3,N=12,J=(l,t)=>!l||!t?l===t:l.rows!==t.rows||l.cols!==t.cols?!1:l.data.every((e,s)=>e===t.data[s]),K=(l,t)=>l.row<t.row||l.row===t.row&&l.col<t.col;export class Calc{cycles=[];computed=0;nowSerial;random;pass=0;stack=[];inCycle=new Set;namesAt=new Set;graph=new T({readsOf:t=>this.readsOf(t),readsAt:(t,e,s,n)=>this.readsAt(t,e,s,n)});graphMade=!1;reports;replica;settled=new Set;moved=[];unsettled=new Set;hiddenBy=new Map;volatiles=new Set;indexes=new U;results=new U;wb;constructor(t,e={}){this.wb=t;const{now:s,random:n}=e;this.reports=e.report===!0,this.replica=e.replica===!0,this.nowSerial=()=>_(s??new Date),this.random=n??Math.random;for(const o of t.sheets)o.expand()}allCells(){return this.wb.sheets.flatMap(t=>[...t.formulas])}filtered(t){return this.hiddenBy.get(t)??Y}calculate(){if(this.replica){for(const e of this.wb.sheets)e.clearSpills();for(const e of this.allCells())e.state=0;return this}this.hiddenBy.clear(),this.indexes.clear(),this.results.clear();for(const e of this.wb.sheets)e.clearSpills();this.moved=[],this.unsettled.clear();const t=this.allCells();for(const e of t)e.state=1;return this.cycles=[],this.inCycle.clear(),this.run(t),this.applyFilters(t),this}applyFilters(t){for(const e of this.wb.sheets){if(e.filters.every(n=>n.columns.size===0))continue;const s=B(e,this);s.size>0&&this.hiddenBy.set(e,s)}if(this.hiddenBy.size!==0&&t.some(e=>Z.test(e.shared.text))){for(const e of t)e.state=1;this.cycles=[],this.inCycle.clear(),this.run([...t])}}value(t,e,s){return this.read(t,e,s)}read(t,e,s){const n=t.at(e,s);return n===void 0?this.ghost(t,e,s):typeof n!="object"||n instanceof m?n:(n.state===1?this.compute(n):n.state===2&&this.circular(n),n.value)}ghost(t,e,s){const n=t.spillAt(e,s);if(!n)return null;if(n.state===1)this.compute(n);else if(n.state===2)return this.circular(n),null;const{array:o}=n;return o&&t.spillAt(e,s)===n?o.at(e-n.row,s-n.col):null}circular(t){const e=this.stack.indexOf(t),s=e<0?[t]:this.stack.slice(e);s.some(n=>this.inCycle.has(n))||this.cycles.push(s);for(const n of s)this.inCycle.add(n)}forget(t,e){this.indexes.changed(t,e),this.results.changed(t,e)}run(t){for(const e of t){const{sheet:s,row:n,col:o}=e;this.forget(s,{top:n,left:o,bottom:n,right:o}),e.spill&&this.forget(s,e.spill)}this.computed=0;for(const e of this.order(t))e.state===1&&this.compute(e);this.spread()}spread(){for(let t=0;this.moved.length>0;t++){const e=this.moved;if(this.moved=[],t>=N*2)break;if(t>=N)for(const{sheet:o,area:r}of e)for(const i of o.spillsIn(r))this.unsettled.add(i);this.build();const s=new Set,n=[...this.unsettled];for(const{sheet:o,area:r}of e)n.push(...this.readersIn(o,r),...o.blockedBy(r));for(;;){const o=n.pop();if(!o)break;s.has(o)||(s.add(o),n.push(...this.readers(o.sheet,o.row,o.col)))}if(s.size!==0){for(const o of s)o.state=1,this.inCycle.delete(o);this.cycles=this.cycles.filter(o=>!o.some(r=>s.has(r))),this.indexes.clear(),this.results.clear();for(const o of this.order([...s]))o.state===1&&this.compute(o)}}for(const t of this.wb.sheets)t.tighten()}spillOut(t,e){const{sheet:s,row:n,col:o}=t,r={top:n,left:o,bottom:n+e.rows-1,right:o+e.cols-1},i=s.spillsIn(r).filter(c=>c!==t);if(r.bottom>=k||r.right>=E||s.holds(r,t)||s.tables.some(c=>O(c.area,r))||s.merges.some(c=>O(c,r))||i.some(c=>K(c,t)))return s.blockSpill(t,r),!1;for(const c of i)this.evict(c,r);return s.placeSpill(t,r),!0}evict(t,e){const{sheet:s,spill:n}=t;n&&(s.clearSpill(t),s.blockSpill(t,e),t.array=void 0,t.value=h.SPILL,this.moved.push({sheet:s,area:n}),this.reports&&this.settled.add(t))}analysisOf(t){return t.analysis??=z(this.wb,t)}readsOf(t){const e=[],s=V(t);for(const n of this.analysisOf(t.shared).reads){const o=n.kind==="ref"?this.resolveRef(n,s):this.structured(n,s);o instanceof u?e.push(o):o instanceof b&&e.push(...o.parts)}return e}readsAt(t,e,s,n){const o={sheet:e,row:s,col:n,dr:s-t.row,dc:n-t.col},r=[];for(const i of this.analysisOf(t).reads){const a=i.kind==="ref"?this.resolveRef(i,o):this.structured(i,o);a instanceof u?r.push(a):a instanceof b?r.push(...a.parts):r.push(void 0)}return r}readsOfFormula(t,e){const s=[],n={sheet:e,row:t.row,col:t.col,dr:0,dc:0};for(const o of this.analysisOf(t).reads){const r=o.kind==="ref"?this.resolveRef(o,n):this.structured(o,n);r instanceof u?s.push(r):r instanceof b&&s.push(...r.parts)}return s}order(t){this.pass+=1;const{pass:e}=this,s=[],n=new Set,o=[],r=i=>{i.mark=e,o.push({cell:i,reads:this.pending(i,n),next:0})};for(const i of t)if(i.mark!==e)for(r(i);;){const a=o[o.length-1];if(!a)break;const c=a.reads[a.next];a.next+=1,c===void 0?(s.push(a.cell),o.pop()):c.mark!==e&&c.state===1&&r(c)}return s}pending(t,e){const s=[];for(const n of this.readsOf(t)){const{sheet:o}=n;if(n.single){const i=o.at(n.top,n.left);i instanceof y&&s.push(i);continue}if(e.has(n.key))continue;e.add(n.key);const r=Math.min(n.right,o.columnsUsed-1);for(let i=n.left;i<=r;i++){if(!o.hasFormulas(i))continue;const[a,c]=o.span(i,n.top,n.bottom);for(let f=a;f<=c;f++){const d=o.at(f,i);d instanceof y&&s.push(d)}}}return s}compute(t){if(this.stack.length>=$){t.value=h.CALC,t.state=0;return}t.state=2,this.stack.push(t),this.computed+=1;const{node:e}=t.shared,s=e?this.top(this.eval(e,V(t))):h.NAME;this.stack.pop(),this.unsettled.has(t)&&!this.inCycle.has(t)&&(this.inCycle.add(t),this.cycles.push([t])),this.settleResult(t,s),t.state=0,this.reports&&this.settled.add(t)}settleResult(t,e){const{sheet:s}=t,n=t.spill,o=t.array;if(s.clearSpill(t),t.array=void 0,e instanceof p){const i=tt(e);i.rows*i.cols<=1||this.spillOut(t,i)?(t.array=i,t.value=i.at(0,0)??0):t.value=h.SPILL}else t.value=e;this.inCycle.has(t)&&(s.clearSpill(t),t.value=h.CYCLE,t.array=void 0);const r=t.spill;(!J(o,t.array)||!Q(n,r))&&(n&&this.moved.push({sheet:s,area:n}),r&&this.moved.push({sheet:s,area:r}))}top(t){const e=this.flat(t);return e===null?0:e}flat(t){return t instanceof u?t.single?this.read(t.sheet,t.top,t.left):this.matrix(t):t instanceof b?h.VALUE:t instanceof M?h.CALC:t instanceof x?null:t}matrix(t){const e=t.bottom===k-1?Math.min(t.bottom,t.sheet.lastRow):t.bottom,s=t.right===E-1?Math.min(t.right,t.sheet.columnsUsed-1):t.right,n=Math.max(0,e-t.top+1),o=Math.max(0,s-t.left+1),r=new Array(n*o);for(let i=0;i<n;i++)for(let a=0;a<o;a++)r[i*o+a]=this.read(t.sheet,t.top+i,t.left+a);return new p(n,o,r)}each(t,e){const{sheet:s}=t,n=Math.min(t.right,s.columnsUsed-1);for(let o=t.left;o<=n;o++){const[r,i]=s.span(o,t.top,t.bottom);for(let a=r;a<=i;a++){const c=s.at(a,o);if(c===void 0){s.spillAt(a,o)&&e(this.read(s,a,o),a,o);continue}e(typeof c=="object"&&!(c instanceof m)?this.read(s,a,o):c,a,o)}}}evaluate(t,e){const{node:s}=t;return s?this.top(this.eval(s,{sheet:e,row:t.row,col:t.col,dr:0,dc:0})):h.NAME}evaluateAt(t,e,s,n){const{node:o}=t;return o?this.top(this.eval(o,{sheet:e,row:s,col:n,dr:s-t.row,dc:n-t.col})):h.NAME}bound(t,e,s){const n=t.abs?t.index:t.index+e;return n<0||n>=s?void 0:n}resolveRef(t,e){const s=t.sheet===void 0?e.sheet:this.wb.sheet(t.sheet);if(!s)return h.REF;let n=0,o=E-1,r=0,i=k-1;if(t.col){const a=this.bound(t.col,e.dc,E),c=this.bound(t.col2??t.col,e.dc,E);if(a===void 0||c===void 0)return h.REF;n=Math.min(a,c),o=Math.max(a,c)}if(t.row){const a=this.bound(t.row,e.dr,k),c=this.bound(t.row2??t.row,e.dr,k);if(a===void 0||c===void 0)return h.REF;r=Math.min(a,c),i=Math.max(a,c)}if(t.lastSheet!==void 0){const a=this.wb.sheet(t.lastSheet);if(!a)return h.REF;const[c,f]=[Math.min(s.index,a.index),Math.max(s.index,a.index)];return new b(this.wb.sheets.slice(c,f+1).map(d=>new u(d,r,n,i,o)))}return new u(s,r,n,i,o)}structured(t,e){const s=t.table===void 0?e.sheet.tableAt(e.row,e.col):this.wb.table(t.table);return s?et(s,t,e):t.table===void 0?h.REF:h.NAME}nameValue(t,e,s){const n=t.toLowerCase();if(s===void 0)for(let c=e.scope;c;c=c.parent){const f=c.names.get(n);if(f!==void 0)return f}const o=s===void 0?e.sheet:this.wb.sheet(s);if(!o)return h.REF;const r=this.wb.name(t,o);if(r){if(!r.formula)return r.value??null;const{node:c}=r.formula;if(!c)return h.NAME;if(this.namesAt.has(n))return h.CYCLE;this.namesAt.add(n);try{return this.eval(c,{sheet:e.sheet,row:0,col:0,dr:0,dc:0})}finally{this.namesAt.delete(n)}}const i=this.wb.table(t);if(i){const{top:c,left:f,bottom:d,right:g}=i.data;return new u(i.sheet,c,f,d,g)}const a=v.get(t.toUpperCase());return a&&a.kind!=="lazy"?st(a,t,e):h.NAME}intersect(t,e){return!(t instanceof u)||t.single?t:t.cols===1&&e.row>=t.top&&e.row<=t.bottom?new u(t.sheet,e.row,t.left,e.row,t.left):t.rows===1&&e.col>=t.left&&e.col<=t.right?new u(t.sheet,t.top,e.col,t.top,e.col):h.VALUE}unary(t,e){const s=this.flat(t);return s instanceof p?new p(s.rows,s.cols,s.data.map(e)):e(s)}operate(t,e,s){const n=this.flat(e),o=this.flat(s);if(n instanceof p||o instanceof p){const r=(i,a)=>S(t,i,a);return H(n instanceof p?n:new p(1,1,[n]),o instanceof p?o:new p(1,1,[o]),r)}return S(t,n,o)}rangeOf(t,e){return t instanceof m?t:e instanceof m?e:!(t instanceof u)||!(e instanceof u)||t.sheet!==e.sheet?h.REF:new u(t.sheet,Math.min(t.top,e.top),Math.min(t.left,e.left),Math.max(t.bottom,e.bottom),Math.max(t.right,e.right))}intersection(t,e){if(t instanceof m)return t;if(e instanceof m)return e;if(!(t instanceof u)||!(e instanceof u))return h.REF;const s=Math.max(t.top,e.top),n=Math.max(t.left,e.left),o=Math.min(t.bottom,e.bottom),r=Math.min(t.right,e.right);return t.sheet!==e.sheet||s>o||n>r?h.NULL:new u(t.sheet,s,n,o,r)}union(t,e){if(t instanceof m)return t;if(e instanceof m)return e;const s=o=>o instanceof u?[o]:o instanceof b?o.parts:[],n=[...s(t),...s(e)];return n.length===0?h.VALUE:new b(n)}eval(t,e){switch(t.kind){case"number":case"string":case"boolean":return t.value;case"error":return m.of(t.code);case"missing":return F;case"ref":return this.resolveRef(t,e);case"structured":return this.structured(t,e);case"name":return this.nameValue(t.name,e,t.sheet);case"paren":return this.eval(t.arg,e);case"unary":{const s=this.eval(t.arg,e);return t.op==="@"?this.intersect(s,e):t.op==="+"?s:this.unary(s,n=>S("*",n,-1))}case"postfix":{const s=this.eval(t.arg,e);if(t.op==="%")return this.unary(s,n=>S("/",n,100));if(s instanceof u&&s.single){const n=s.sheet.at(s.top,s.left);if(n instanceof y){this.read(s.sheet,s.top,s.left);const{spill:o}=n;if(o)return new u(s.sheet,o.top,o.left,o.bottom,o.right);if(n.array)return s}}return h.REF}case"binary":{const s=this.eval(t.left,e),n=this.eval(t.right,e);return t.op===":"?this.rangeOf(s,n):t.op===" "?this.intersection(s,n):t.op===","?this.union(s,n):this.operate(t.op,s,n)}case"array":return new p(t.rows.length,t.rows[0]?.length??0,t.rows.flatMap(s=>s.map(n=>{const o=this.eval(n,e);return o instanceof p||o instanceof u||o instanceof b||o instanceof M||o instanceof x?h.VALUE:o})));case"invoke":{const s=this.eval(t.callee,e);return s instanceof M?this.invoke(s,t.args,e):s instanceof m?s:h.VALUE}case"call":return this.call(t.name,t.args,e)}}invoke(t,e,s){return this.apply(t,e.map(n=>this.eval(n,s)))}apply(t,e){if(t.named!==void 0){const n=v.get(t.named);return!n||n.kind==="lazy"?h.NAME:e.length<n.min||e.length>n.max?h.VALUE:this.applyFunction(n,e,t.frame)}if(e.length>t.params.length)return h.VALUE;const s=new Map;return t.params.forEach((n,o)=>{s.set(n,e[o]??F)}),this.eval(t.body,{...t.frame,scope:{names:s,...t.frame.scope?{parent:t.frame.scope}:{}}})}call(t,e,s){const n=v.get(t);if(!n){const o=this.nameValue(t,s);return o instanceof M?this.invoke(o,e,s):h.NAME}return e.length<n.min||e.length>n.max?h.VALUE:n.kind==="lazy"?n.call(e,{calc:this,frame:s}):this.applyFunction(n,e.map(o=>this.eval(o,s)),s)}applyFunction(t,e,s){const n={calc:this,frame:s};if(t.kind==="lazy")return h.VALUE;if(t.kind==="raw")return j(t.params,e.length)?this.over(e,r=>X(t.params,r)==="value",r=>t.call(r,n)):h.VALUE;const o=t.errors===!0;return this.over(e,()=>!0,r=>{const i=r.map(a=>a instanceof x?void 0:a);if(!o){for(const a of i)if(a instanceof m)return a}return t.call(...i)})}over(t,e,s){const n=[],o=new Map;for(const[f,d]of t.entries()){if(!e(f)||d instanceof x){n.push(d);continue}const g=this.flat(d);g instanceof p&&o.set(f,g),n.push(g)}if(o.size===0)return s(n);const r=[...o.values()],i=Math.max(...r.map(f=>f.rows)),a=Math.max(...r.map(f=>f.cols));if(i===0||a===0)return h.CALC;const c=new Array(i*a);for(let f=0;f<i;f++)for(let d=0;d<a;d++){const g=n.map((C,A)=>{const w=o.get(A);if(!w)return C;const L=w.rows===1?0:f,R=w.cols===1?0:d;return L<w.rows&&R<w.cols?w.at(L,R):h.NA});c[f*a+d]=this.element(s(g))}return new p(i,a,c)}element(t){const e=this.flat(t);return e instanceof p?e.rows>0&&e.cols>0?e.at(0,0):h.CALC:e}scalar(t,e){if(t instanceof x)return null;if(t instanceof u){const s=this.intersect(t,e);return s instanceof u?this.read(s.sheet,s.top,s.left):h.VALUE}return t instanceof p?t.at(0,0):t instanceof b||t instanceof M?h.VALUE:t}recalculate(){if(this.replica)return this;const t=this.allCells().filter(e=>this.analysisOf(e.shared).volatile);return this.mark(t),this}build(){if(this.graphMade)return;this.graphMade=!0;const t=this.allCells();for(const e of t)this.analysisOf(e.shared).volatile&&this.volatiles.add(e);this.graph.build(t)}addEdges(t){this.analysisOf(t.shared).volatile&&this.volatiles.add(t),this.graph.add(t)}removeEdges(t){this.volatiles.delete(t),this.graph.remove(t)}readersIn(t,e){return this.graph.readersIn(t,e)}readers(t,e,s){return this.build(),this.graph.readers(t,e,s)}precedents(t){return this.readsOf(t)}mark(t){this.build();const e=new Set(t),s=[...t];for(;;){const n=s.pop();if(!n)break;for(const o of this.readers(n.sheet,n.row,n.col))e.has(o)||(e.add(o),s.push(o))}this.settle(e)}settle(t){for(const e of t)e.state=1,this.inCycle.delete(e);this.cycles=this.cycles.filter(e=>!e.some(s=>t.has(s))),this.run([...t])}set(t,e,s,n){const o=typeof n=="string"&&n.length>1&&n.startsWith("=")?new y(new I(n,e,s,{file:""}),t,e,s):void 0;this.change([{sheet:t,row:e,col:s,slot:o??n??void 0}])}prepare(){this.build()}change(t){if(this.replica){for(const{sheet:o,row:r,col:i,slot:a}of t){const c=o.at(r,i);c instanceof y&&o.clearSpill(c),o.replace(r,i,a),a instanceof y&&(a.state=0)}return}this.build();const e=new Set,s=[...this.unsettled];this.unsettled.clear();const n=new Set;for(const{sheet:o,row:r,col:i,slot:a}of t){const c=o.at(r,i);c instanceof y&&(n.add(c),this.inCycle.delete(c),this.removeEdges(c),e.delete(c),c.spill&&this.moved.push({sheet:o,area:c.spill}),o.clearSpill(c));const f=o.spillAt(r,i);f&&s.push(f),s.push(...o.blockedBy({top:r,left:i,bottom:r,right:i})),o.replace(r,i,a),this.forget(o,{top:r,left:i,bottom:r,right:i}),a instanceof y&&(this.addEdges(a),e.add(a))}n.size>0&&(this.cycles=this.cycles.filter(o=>!o.some(r=>n.has(r))));for(const{sheet:o,row:r,col:i}of t)s.push(...this.readers(o,r,i));for(s.push(...this.volatiles);;){const o=s.pop();if(!o)break;e.has(o)||(e.add(o),s.push(...this.readers(o.sheet,o.row,o.col)))}this.settle(e)}setName(t,e){const s=this.wb.name(t);if(!s)throw new Error(`There is no name ${t}.`);s.value=e,delete s.formula;const n=t.toLowerCase();this.mark(this.allCells().filter(o=>this.analysisOf(o.shared).names.has(n)))}takeComputed(){const t=[];for(const e of this.settled)t.push({sheet:e.sheet.index,row:e.row,col:e.col,value:P(e.value),...e.array?{array:W(e.array)}:{},...e.spill?{spill:e.spill}:{},...e.blocked?{blocked:e.blocked}:{}});return this.settled.clear(),{cells:t,cycles:this.cycles.map(e=>e.map(s=>({sheet:s.sheet.index,row:s.row,col:s.col}))),hidden:[...this.hiddenBy].map(([e,s])=>({sheet:e.index,rows:[...s]}))}}receive(t){for(const s of t.cells){const n=this.wb.sheets[s.sheet],o=n?.at(s.row,s.col);!n||!(o instanceof y)||(n.clearSpill(o),o.value=G(s.value),o.array=s.array?D(s.array):void 0,s.spill&&n.placeSpill(o,s.spill),s.blocked&&n.blockSpill(o,s.blocked),o.state=0)}this.cycles=t.cycles.flatMap(s=>{const n=s.flatMap(o=>{const r=this.wb.sheets[o.sheet]?.at(o.row,o.col);return r instanceof y?[r]:[]});return n.length>0?[n]:[]});const e=new Map(this.hiddenBy);this.hiddenBy.clear();for(const{sheet:s,rows:n}of t.hidden){const o=this.wb.sheets[s];if(!o)continue;const r=e.get(o),i=r?.size===n.length&&n.every(a=>r.has(a));this.hiddenBy.set(o,i?r:new Set(n))}for(const s of this.wb.sheets)s.tighten()}numberOf(t){return q(t)}}const Q=(l,t)=>l?.top===t?.top&&l?.left===t?.left&&l?.bottom===t?.bottom&&l?.right===t?.right;function tt(l){return l.data.some(t=>t===null)?new p(l.rows,l.cols,l.data.map(t=>t??0)):l}function V(l){return{sheet:l.sheet,row:l.row,col:l.col,dr:l.row-l.shared.row,dc:l.col-l.shared.col}}function et(l,t,e){const{sheet:s}=l,n=l.area;let o=n.left,r=n.right;const[i,a]=t.columns;if(i!==void 0){const A=l.column(i),w=a===void 0?A:l.column(a);if(!A||!w)return h.REF;o=n.left+Math.min(A.index,w.index),r=n.left+Math.max(A.index,w.index)}if(t.thisRow)return e.sheet!==s||e.row<=n.top||e.row>n.bottom?h.VALUE:new u(s,e.row,o,e.row,r);const c=t.sections;if(c.includes("#Totals"))return h.REF;const f=c.includes("#All")||c.includes("#Headers"),d=c.length===0||c.includes("#All")||c.includes("#Data"),g=f?n.top:n.top+1,C=d?n.bottom:n.top;return new u(s,g,o,C,r)}function st(l,t,e){return new M([],{kind:"call",name:l.name,written:t,args:[]},e,l.name)}
2
+ import{MAX_COLUMNS as v,MAX_ROWS as C,overlaps as N}from"../address.js";import{CellError as b,ERR as h,Matrix as m,serialOfDate as G}from"../values.js";import{filteredRows as W}from"../filter.js";import{FormulaCell as k,SharedFormula as P}from"../workbook.js";import{analyse as q}from"./analyse.js";import{Graph as H}from"./graph.js";import{fromWire as $,matrixFromWire as j,matrixToWire as K,toWire as X}from"./remote.js";import{RangeCache as U}from"./indexes.js";import{binary as O,mapBoth as Y,toNumber as Z}from"./coerce.js";import{FUNCTIONS as _}from"./functions/index.js";import{fillsGroup as J,paramAt as Q}from"./functions/define.js";import{Lambda as S,MISSING as T,Missing as L,Reference as d,Union as x,pushAll as M}from"./types.js";export function seededRandom(l){let t=2166136261;for(let e=0;e<l.length;e++)t=Math.imul(t^l.charCodeAt(e),16777619);return()=>{t=t+1831565813|0;let e=Math.imul(t^t>>>15,1|t);return e=e+Math.imul(e^e>>>7,61|e)^e,((e^e>>>14)>>>0)/4294967296}}const tt=new Set,et=/\b(?:SUBTOTAL|AGGREGATE)\(/iu,I=2e3,st=50,nt=1024;class V extends Error{cell;constructor(t){super("computed first"),this.cell=t}}const B=12;export const ITERATION_DEFAULTS={max_iterations:100,max_change:.001};function ot(l,t){return typeof l=="number"&&typeof t=="number"?Math.abs(t-l):l===t||l instanceof b&&t instanceof b&&l.code===t.code?0:1/0}const it=(l,t)=>!l||!t?l===t:l.rows!==t.rows||l.cols!==t.cols?!1:l.data.every((e,s)=>e===t.data[s]),rt=(l,t)=>l.row<t.row||l.row===t.row&&l.col<t.col;export class Calc{cycles=[];computed=0;iterated;nowSerial;random;pass=0;lambdaSlots=0;stack=[];inCycle=new Set;looping=new Set;tablesOf;inTables=!1;loop=new Set;around=new Set;namesAt=new Set;graph=new H({readsOf:t=>this.readsOf(t),readsAt:(t,e,s,o)=>this.readsAt(t,e,s,o)});graphMade=!1;reports;replica;settled=new Set;moved=[];unsettled=new Set;hiddenBy=new Map;volatiles=new Set;indexes=new U;results=new U;wb;constructor(t,e={}){this.wb=t;const{now:s,random:o}=e;this.reports=e.report===!0,this.replica=e.replica===!0,this.nowSerial=()=>G(s??new Date),this.random=o??Math.random;for(const n of t.sheets)n.expand()}allCells(){return this.wb.sheets.flatMap(t=>[...t.formulas])}filtered(t){return this.hiddenBy.get(t)??tt}calculate(){if(this.replica){for(const e of this.wb.sheets)e.clearSpills();for(const e of this.allCells())e.state=0;return this}this.hiddenBy.clear(),this.indexes.clear(),this.results.clear();for(const e of this.wb.sheets)e.clearSpills();this.moved=[],this.unsettled.clear();const t=this.allCells();for(const e of t)e.state=1;return this.cycles=[],this.inCycle.clear(),this.looping.clear(),this.loop=new Set,this.around.clear(),this.tablesOf=void 0,this.iterated=void 0,this.run(t),this.applyFilters(t),this}iteration(){const t=this.wb.file.iteration;if(t!==void 0)return{max_iterations:(t===!0?void 0:t.max_iterations)??ITERATION_DEFAULTS.max_iterations,max_change:(t===!0?void 0:t.max_change)??ITERATION_DEFAULTS.max_change}}applyFilters(t){for(const e of this.wb.sheets){if(e.filters.every(o=>o.columns.size===0))continue;const s=W(e,this);s.size>0&&this.hiddenBy.set(e,s)}if(this.hiddenBy.size!==0&&t.some(e=>et.test(e.shared.text))){for(const e of t)e.state=1;this.cycles=[],this.inCycle.clear(),this.run([...t])}}value(t,e,s){return this.read(t,e,s)}read(t,e,s){const o=t.at(e,s);return o===void 0?this.ghost(t,e,s):typeof o!="object"||o instanceof b?o:(o.state===1?this.computeNow(o):o.state===2&&this.circular(o),o.value)}ghost(t,e,s){const o=t.spillAt(e,s);if(!o)return null;if(o.state===1)this.computeNow(o);else if(o.state===2)return this.circular(o),null;const{array:n}=o;return n&&t.spillAt(e,s)===o?n.at(e-o.row,s-o.col):null}circular(t){const e=this.stack.indexOf(t),s=e<0?[t]:this.stack.slice(e);if(this.iteration()){for(const o of s)this.looping.add(o);return}s.some(o=>this.inCycle.has(o))||this.cycles.push(s);for(const o of s)this.inCycle.add(o)}forget(t,e){this.indexes.changed(t,e),this.results.changed(t,e)}run(t){for(const e of t){const{sheet:s,row:o,col:n}=e;this.forget(s,{top:o,left:n,bottom:o,right:n}),e.spill&&this.forget(s,e.spill)}t.some(e=>this.loop.has(e))&&(this.iterated=void 0),this.computed=0;for(const e of this.order(t))e.state===1&&this.computeNow(e);this.spread(),this.iterate(),this.tables()}tableCells(){if(this.tablesOf)return this.tablesOf;const t=new Map;for(const e of this.allCells())if(R(e.shared.node)){const s=t.get(e.shared);s?s.push(e):t.set(e.shared,[e])}return this.tablesOf=t,t}tables(){if(this.replica||this.inTables)return;const t=this.tableCells();if(t.size!==0){this.inTables=!0;try{const e=[];for(const[n,i]of t)this.table(n,i),e.push(...i);const s=new Set(e),o=new Set;for(const n of e)for(const i of this.readers(n.sheet,n.row,n.col))s.has(i)||o.add(i);o.size>0&&this.mark([...o])}finally{this.inTables=!1}}}operations(t){const e=new Map,s=(n,i)=>{const r=n===void 0?void 0:this.eval(n,i);return r instanceof d&&r.single?r:void 0},o=t.map(n=>{const i=n.shared.node;if(i?.kind!=="call")return h.REF;const r=F(n),a=s(i.args[0],r);if(!a)return h.REF;const c=[];for(let f=1;f+1<i.args.length;f+=2){const u=s(i.args[f],r),w=s(i.args[f+1],r);if(!u||!w)return h.REF;const E=`${String(u.sheet.index)}:${String(u.top)}:${String(u.left)}`;e.has(E)||e.set(E,{sheet:u.sheet,row:u.top,col:u.left,slot:u.sheet.at(u.top,u.left)});const A=this.read(w.sheet,w.top,w.left);c.push({sheet:u.sheet,row:u.top,col:u.left,slot:A??void 0})}return this.change(c),this.read(a.sheet,a.top,a.left)});e.size>0&&this.change([...e.values()]),t.forEach((n,i)=>{n.value=o[i]??null,this.reports&&this.settled.add(n)})}table(t,e){const[s]=e,o=t.node;if(!s||o?.kind!=="call")return;if(o.args.length>2){this.operations(e);return}const{sheet:n}=s,i=t.row,r=t.col,a={sheet:n,row:i,col:r,dr:0,dc:0},c=p=>{if(p===void 0||p.kind==="missing")return null;const g=this.eval(p,a);return g instanceof d&&g.single?g:void 0},f=c(o.args[0]),u=c(o.args[1]),w=p=>{for(const g of e)g.value=p,this.reports&&this.settled.add(g)};if(f===void 0||u===void 0||f===null&&u===null||i===0||r===0){w(h.REF);return}const A=[f,u].filter(p=>p!==null).map(p=>({sheet:p.sheet,row:p.top,col:p.left,slot:p.sheet.at(p.top,p.left)})),y=e.map(p=>{const g=[];f&&g.push({sheet:f.sheet,row:f.top,col:f.left,slot:this.read(n,i-1,p.col)??void 0}),u&&g.push({sheet:u.sheet,row:u.top,col:u.left,slot:this.read(n,p.row,r-1)??void 0}),this.change(g);const[z,D]=f&&u?[i-1,r-1]:u?[i-1,p.col]:[p.row,r-1];return this.read(n,z,D)});this.change(A),e.forEach((p,g)=>{p.value=y[g]??null,this.reports&&this.settled.add(p)})}iterate(){const t=this.iteration();if(!t||this.looping.size===0){this.looping.clear();return}for(const i of this.looping)this.around.add(i);const e=this.downstream([...this.looping]),s=[...e];let o=1,n=1/0;for(;o<t.max_iterations;){const i=s.map(r=>r.value);this.looping.clear(),this.indexes.clear(),this.results.clear();for(const r of s)r.state=1;for(const r of this.order(s))r.state===1&&this.compute(r);if(o+=1,n=0,s.forEach((r,a)=>{n=Math.max(n,ot(i[a]??null,r.value))}),n<t.max_change)break}this.looping.clear(),this.spread(),this.looping.clear(),this.loop=e,this.iterated={cells:s.length,iterations:o,change:n}}iterates(t){return this.loop.has(t)}circularCells(){return[...this.around]}downstream(t){const e=new Set(t),s=[...t];for(;;){const o=s.pop();if(!o)break;for(const n of this.readers(o.sheet,o.row,o.col))e.has(n)||(e.add(n),s.push(n))}return e}spread(){for(let t=0;this.moved.length>0;t++){const e=this.moved;if(this.moved=[],t>=B*2)break;if(t>=B)for(const{sheet:n,area:i}of e)for(const r of n.spillsIn(i))this.unsettled.add(r);this.build();const s=new Set,o=[...this.unsettled];for(const{sheet:n,area:i}of e)M(o,this.readersIn(n,i)),M(o,n.blockedBy(i));for(;;){const n=o.pop();if(!n)break;s.has(n)||(s.add(n),M(o,this.readers(n.sheet,n.row,n.col)))}if(s.size!==0){for(const n of s)n.state=1,this.inCycle.delete(n);this.cycles=this.cycles.filter(n=>!n.some(i=>s.has(i))),this.indexes.clear(),this.results.clear();for(const n of this.order([...s]))n.state===1&&this.computeNow(n)}}for(const t of this.wb.sheets)t.tighten()}spillOut(t,e){const{sheet:s,row:o,col:n}=t,i={top:o,left:n,bottom:o+e.rows-1,right:n+e.cols-1},r=s.spillsIn(i).filter(c=>c!==t);if(i.bottom>=C||i.right>=v||s.holds(i,t)||s.tables.some(c=>N(c.area,i))||s.merges.some(c=>N(c,i))||r.some(c=>rt(c,t)))return s.blockSpill(t,i),!1;for(const c of r)this.evict(c,i);return s.placeSpill(t,i),!0}evict(t,e){const{sheet:s,spill:o}=t;o&&(s.clearSpill(t),s.blockSpill(t,e),t.array=void 0,t.value=h.SPILL,this.moved.push({sheet:s,area:o}),this.reports&&this.settled.add(t))}analysisOf(t){return t.analysis??=q(this.wb,t)}readsOf(t){const e=[],s=F(t);for(const o of this.analysisOf(t.shared).reads){const n=o.kind==="ref"?this.resolveRef(o,s):this.structured(o,s);n instanceof d?e.push(n):n instanceof x&&e.push(...n.parts)}return e}readsAt(t,e,s,o){const n={sheet:e,row:s,col:o,dr:s-t.row,dc:o-t.col},i=[];for(const r of this.analysisOf(t).reads){const a=r.kind==="ref"?this.resolveRef(r,n):this.structured(r,n);a instanceof d?i.push(a):a instanceof x?i.push(...a.parts):i.push(void 0)}return i}readsOfFormula(t,e){const s=[],o={sheet:e,row:t.row,col:t.col,dr:0,dc:0};for(const n of this.analysisOf(t).reads){const i=n.kind==="ref"?this.resolveRef(n,o):this.structured(n,o);i instanceof d?s.push(i):i instanceof x&&s.push(...i.parts)}return s}order(t){this.pass+=1;const{pass:e}=this,s=[],o=new Set,n=[],i=r=>{r.mark=e,n.push({cell:r,reads:this.pending(r,o),next:0})};for(const r of t)if(r.mark!==e)for(i(r);;){const a=n[n.length-1];if(!a)break;const c=a.reads[a.next];a.next+=1,c===void 0?(s.push(a.cell),n.pop()):c.mark!==e&&c.state===1&&i(c)}return s}pending(t,e){const s=[];for(const o of this.readsOf(t)){const{sheet:n}=o;if(o.single){const r=n.at(o.top,o.left);r instanceof k&&s.push(r);continue}if(e.has(o.key))continue;e.add(o.key);const i=Math.min(o.right,n.columnsUsed-1);for(let r=o.left;r<=i;r++){if(!n.hasFormulas(r))continue;const[a,c]=n.span(r,o.top,o.bottom);for(let f=a;f<=c;f++){const u=n.at(f,r);u instanceof k&&s.push(u)}}}return s}computeNow(t){if(this.stack.length>0){if(this.stack.length>=st&&this.stack.length<I)throw new V(t);this.compute(t);return}const e=[t],s=new Set([t]);for(;e.length>0;){const o=e[e.length-1];if(o===void 0)break;if(o.state!==1){e.pop();continue}try{this.compute(o),e.pop()}catch(n){if(!(n instanceof V))throw n;for(const i of this.stack)i.state=1;if(this.stack.length=0,s.has(n.cell)){n.cell.value=h.CALC,n.cell.state=0;continue}s.add(n.cell),e.push(n.cell)}}}compute(t){if(this.stack.length>=I){t.value=h.CALC,t.state=0;return}t.state=2,this.stack.push(t),this.computed+=1;const{node:e}=t.shared,s=R(e)?t.value:e?this.top(this.eval(e,F(t))):h.NAME;this.stack.pop(),this.unsettled.has(t)&&!this.inCycle.has(t)&&(this.inCycle.add(t),this.cycles.push([t])),this.settleResult(t,s),t.state=0,this.reports&&this.settled.add(t)}settleResult(t,e){const{sheet:s}=t,o=t.spill,n=t.array;if(s.clearSpill(t),t.array=void 0,e instanceof m){const r=ct(e);r.rows*r.cols<=1||this.spillOut(t,r)?(t.array=r,t.value=r.at(0,0)??0):t.value=h.SPILL}else t.value=e;this.inCycle.has(t)&&(s.clearSpill(t),t.value=h.CYCLE,t.array=void 0);const i=t.spill;(!it(n,t.array)||!at(o,i))&&(o&&this.moved.push({sheet:s,area:o}),i&&this.moved.push({sheet:s,area:i}))}top(t){const e=this.flat(t);return e===null?0:e}flat(t){return t instanceof d?t.single?this.read(t.sheet,t.top,t.left):this.matrix(t):t instanceof x?h.VALUE:t instanceof S?h.CALC:t instanceof L?null:t}matrix(t){const e=t.bottom===C-1?Math.min(t.bottom,t.sheet.lastRow):t.bottom,s=t.right===v-1?Math.min(t.right,t.sheet.columnsUsed-1):t.right,o=Math.max(0,e-t.top+1),n=Math.max(0,s-t.left+1),i=new Array(o*n);for(let r=0;r<o;r++)for(let a=0;a<n;a++)i[r*n+a]=this.read(t.sheet,t.top+r,t.left+a);return new m(o,n,i)}each(t,e){const{sheet:s}=t,o=Math.min(t.right,s.columnsUsed-1);for(let n=t.left;n<=o;n++){const[i,r]=s.span(n,t.top,t.bottom);for(let a=i;a<=r;a++){const c=s.at(a,n);if(c===void 0){s.spillAt(a,n)&&e(this.read(s,a,n),a,n);continue}e(typeof c=="object"&&!(c instanceof b)?this.read(s,a,n):c,a,n)}}}evaluate(t,e){const{node:s}=t;return s?this.top(this.eval(s,{sheet:e,row:t.row,col:t.col,dr:0,dc:0})):h.NAME}evaluateAt(t,e,s,o){const{node:n}=t;return n?this.top(this.eval(n,{sheet:e,row:s,col:o,dr:s-t.row,dc:o-t.col})):h.NAME}bound(t,e,s){const o=t.abs?t.index:t.index+e;return o<0||o>=s?void 0:o}resolveRef(t,e){const s=t.sheet===void 0?e.sheet:this.wb.sheet(t.sheet);if(!s)return h.REF;let o=0,n=v-1,i=0,r=C-1;if(t.col){const a=this.bound(t.col,e.dc,v),c=this.bound(t.col2??t.col,e.dc,v);if(a===void 0||c===void 0)return h.REF;o=Math.min(a,c),n=Math.max(a,c)}if(t.row){const a=this.bound(t.row,e.dr,C),c=this.bound(t.row2??t.row,e.dr,C);if(a===void 0||c===void 0)return h.REF;i=Math.min(a,c),r=Math.max(a,c)}if(t.lastSheet!==void 0){const a=this.wb.sheet(t.lastSheet);if(!a)return h.REF;const[c,f]=[Math.min(s.index,a.index),Math.max(s.index,a.index)];return new x(this.wb.sheets.slice(c,f+1).map(u=>new d(u,i,o,r,n)))}return new d(s,i,o,r,n)}structured(t,e){const s=t.table===void 0?e.sheet.tableAt(e.row,e.col):this.wb.table(t.table);return s?lt(s,t,e):t.table===void 0?h.REF:h.NAME}nameValue(t,e,s){const o=t.toLowerCase();if(s===void 0)for(let c=e.scope;c;c=c.parent){const f=c.names.get(o);if(f!==void 0)return f}const n=s===void 0?e.sheet:this.wb.sheet(s);if(!n)return h.REF;const i=this.wb.name(t,n);if(i){if(!i.formula)return i.value??null;const{node:c}=i.formula;if(!c)return h.NAME;if(this.namesAt.has(o))return h.CYCLE;this.namesAt.add(o);try{return this.eval(c,{sheet:e.sheet,row:0,col:0,dr:0,dc:0})}finally{this.namesAt.delete(o)}}const r=this.wb.table(t);if(r){const{top:c,left:f,bottom:u,right:w}=r.data;return new d(r.sheet,c,f,u,w)}const a=_.get(t.toUpperCase());return a&&a.kind!=="lazy"?ht(a,t,e):h.NAME}intersect(t,e){return!(t instanceof d)||t.single?t:t.cols===1&&e.row>=t.top&&e.row<=t.bottom?new d(t.sheet,e.row,t.left,e.row,t.left):t.rows===1&&e.col>=t.left&&e.col<=t.right?new d(t.sheet,t.top,e.col,t.top,e.col):h.VALUE}unary(t,e){const s=this.flat(t);return s instanceof m?new m(s.rows,s.cols,s.data.map(e)):e(s)}operate(t,e,s){const o=this.flat(e),n=this.flat(s);if(o instanceof m||n instanceof m){const i=(r,a)=>O(t,r,a);return Y(o instanceof m?o:new m(1,1,[o]),n instanceof m?n:new m(1,1,[n]),i)}return O(t,o,n)}rangeOf(t,e){return t instanceof b?t:e instanceof b?e:!(t instanceof d)||!(e instanceof d)||t.sheet!==e.sheet?h.REF:new d(t.sheet,Math.min(t.top,e.top),Math.min(t.left,e.left),Math.max(t.bottom,e.bottom),Math.max(t.right,e.right))}intersection(t,e){if(t instanceof b)return t;if(e instanceof b)return e;if(!(t instanceof d)||!(e instanceof d))return h.REF;const s=Math.max(t.top,e.top),o=Math.max(t.left,e.left),n=Math.min(t.bottom,e.bottom),i=Math.min(t.right,e.right);return t.sheet!==e.sheet||s>n||o>i?h.NULL:new d(t.sheet,s,o,n,i)}union(t,e){if(t instanceof b)return t;if(e instanceof b)return e;const s=n=>n instanceof d?[n]:n instanceof x?n.parts:[],o=[...s(t),...s(e)];return o.length===0?h.VALUE:new x(o)}eval(t,e){switch(t.kind){case"number":case"string":case"boolean":return t.value;case"error":return b.of(t.code);case"missing":return T;case"ref":return this.resolveRef(t,e);case"structured":return this.structured(t,e);case"name":return this.nameValue(t.name,e,t.sheet);case"paren":return this.eval(t.arg,e);case"unary":{const s=this.eval(t.arg,e);return t.op==="@"?this.intersect(s,e):t.op==="+"?s:this.unary(s,o=>O("*",o,-1))}case"postfix":{const s=this.eval(t.arg,e);if(t.op==="%")return this.unary(s,o=>O("/",o,100));if(s instanceof d&&s.single){const o=s.sheet.at(s.top,s.left);if(o instanceof k){this.read(s.sheet,s.top,s.left);const{spill:n}=o;if(n)return new d(s.sheet,n.top,n.left,n.bottom,n.right);if(o.array)return s}}return h.REF}case"binary":{const s=this.eval(t.left,e),o=this.eval(t.right,e);return t.op===":"?this.rangeOf(s,o):t.op===" "?this.intersection(s,o):t.op===","?this.union(s,o):this.operate(t.op,s,o)}case"array":return new m(t.rows.length,t.rows[0]?.length??0,t.rows.flatMap(s=>s.map(o=>{const n=this.eval(o,e);return n instanceof m||n instanceof d||n instanceof x||n instanceof S||n instanceof L?h.VALUE:n})));case"invoke":{const s=this.eval(t.callee,e);return s instanceof S?this.invoke(s,t.args,e):s instanceof b?s:h.VALUE}case"call":return this.call(t.name,t.args,e)}}invoke(t,e,s){return this.apply(t,e.map(o=>this.eval(o,s)))}apply(t,e){if(t.named!==void 0){const n=_.get(t.named);return!n||n.kind==="lazy"?h.NAME:e.length<n.min||e.length>n.max?h.VALUE:this.applyFunction(n,e,t.frame)}if(e.length>t.params.length)return h.VALUE;const s=t.params.length+1;if(this.lambdaSlots+s>nt)return h.NUM;const o=new Map;t.params.forEach((n,i)=>{o.set(n,e[i]??T)}),this.lambdaSlots+=s;try{return this.eval(t.body,{...t.frame,scope:{names:o,...t.frame.scope?{parent:t.frame.scope}:{}}})}finally{this.lambdaSlots-=s}}call(t,e,s){const o=_.get(t);if(!o){const n=this.nameValue(t,s);return n instanceof S?this.invoke(n,e,s):h.NAME}return e.length<o.min||e.length>o.max?h.VALUE:o.kind==="lazy"?o.call(e,{calc:this,frame:s}):this.applyFunction(o,e.map(n=>this.eval(n,s)),s)}applyFunction(t,e,s){const o={calc:this,frame:s};if(t.kind==="lazy")return h.VALUE;if(t.kind==="raw")return J(t.params,e.length)?this.over(e,i=>Q(t.params,i)==="value",i=>t.call(i,o)):h.VALUE;const n=t.errors===!0;return this.over(e,()=>!0,i=>{const r=i.map(a=>a instanceof L?void 0:a);if(!n){for(const a of r)if(a instanceof b)return a}return t.call(...r)})}over(t,e,s){const o=[],n=new Map;for(const[f,u]of t.entries()){if(!e(f)||u instanceof L){o.push(u);continue}const w=this.flat(u);w instanceof m&&n.set(f,w),o.push(w)}if(n.size===0)return s(o);const i=[...n.values()],r=Math.max(...i.map(f=>f.rows)),a=Math.max(...i.map(f=>f.cols));if(r===0||a===0)return h.CALC;const c=new Array(r*a);for(let f=0;f<r;f++)for(let u=0;u<a;u++){const w=o.map((E,A)=>{const y=n.get(A);if(!y)return E;const p=y.rows===1?0:f,g=y.cols===1?0:u;return p<y.rows&&g<y.cols?y.at(p,g):h.NA});c[f*a+u]=this.element(s(w))}return new m(r,a,c)}element(t){const e=this.flat(t);return e instanceof m?e.rows>0&&e.cols>0?e.at(0,0):h.CALC:e}scalar(t,e){if(t instanceof L)return null;if(t instanceof d){const s=this.intersect(t,e);return s instanceof d?this.read(s.sheet,s.top,s.left):h.VALUE}return t instanceof m?t.at(0,0):t instanceof x||t instanceof S?h.VALUE:t}recalculate(){if(this.replica)return this;const t=this.allCells().filter(e=>this.analysisOf(e.shared).volatile);return this.mark(t),this}build(){if(this.graphMade)return;this.graphMade=!0;const t=this.allCells();for(const e of t)this.analysisOf(e.shared).volatile&&this.volatiles.add(e);this.graph.build(t)}addEdges(t){this.analysisOf(t.shared).volatile&&this.volatiles.add(t),this.graph.add(t)}removeEdges(t){this.volatiles.delete(t),this.graph.remove(t)}readersIn(t,e){return this.graph.readersIn(t,e)}readers(t,e,s){return this.build(),this.graph.readers(t,e,s)}precedents(t){return this.readsOf(t)}mark(t){this.build();const e=new Set(t),s=[...t];for(;;){const o=s.pop();if(!o)break;for(const n of this.readers(o.sheet,o.row,o.col))e.has(n)||(e.add(n),s.push(n))}this.settle(e)}settle(t){for(const e of t)e.state=1,this.inCycle.delete(e);this.cycles=this.cycles.filter(e=>!e.some(s=>t.has(s))),this.run([...t])}set(t,e,s,o){const n=typeof o=="string"&&o.length>1&&o.startsWith("=")?new k(new P(o,e,s,{file:""}),t,e,s):void 0;this.change([{sheet:t,row:e,col:s,slot:n??o??void 0}])}prepare(){this.build()}change(t){if(this.replica){for(const{sheet:n,row:i,col:r,slot:a}of t){const c=n.at(i,r);c instanceof k&&n.clearSpill(c),n.replace(i,r,a),a instanceof k&&(a.state=0)}return}this.build();const e=new Set,s=[...this.unsettled];this.unsettled.clear();const o=new Set;for(const{sheet:n,row:i,col:r,slot:a}of t){const c=n.at(i,r);(c instanceof k&&R(c.shared.node)||a instanceof k&&R(a.shared.node))&&(this.tablesOf=void 0),c instanceof k&&(o.add(c),this.inCycle.delete(c),this.removeEdges(c),e.delete(c),c.spill&&this.moved.push({sheet:n,area:c.spill}),n.clearSpill(c));const f=n.spillAt(i,r);f&&s.push(f),s.push(...n.blockedBy({top:i,left:r,bottom:i,right:r})),n.replace(i,r,a),this.forget(n,{top:i,left:r,bottom:i,right:r}),a instanceof k&&(this.addEdges(a),e.add(a))}o.size>0&&(this.cycles=this.cycles.filter(n=>!n.some(i=>o.has(i))));for(const{sheet:n,row:i,col:r}of t)M(s,this.readers(n,i,r));for(M(s,this.volatiles);;){const n=s.pop();if(!n)break;e.has(n)||(e.add(n),M(s,this.readers(n.sheet,n.row,n.col)))}this.settle(e)}setName(t,e){const s=this.wb.name(t);if(!s)throw new Error(`There is no name ${t}.`);s.value=e,delete s.formula;const o=t.toLowerCase();this.mark(this.allCells().filter(n=>this.analysisOf(n.shared).names.has(o)))}takeComputed(){const t=[];for(const e of this.settled)t.push({sheet:e.sheet.index,row:e.row,col:e.col,value:X(e.value),...e.array?{array:K(e.array)}:{},...e.spill?{spill:e.spill}:{},...e.blocked?{blocked:e.blocked}:{}});return this.settled.clear(),{cells:t,cycles:this.cycles.map(e=>e.map(s=>({sheet:s.sheet.index,row:s.row,col:s.col}))),hidden:[...this.hiddenBy].map(([e,s])=>({sheet:e.index,rows:[...s]}))}}receive(t){for(const s of t.cells){const o=this.wb.sheets[s.sheet],n=o?.at(s.row,s.col);!o||!(n instanceof k)||(o.clearSpill(n),n.value=$(s.value),n.array=s.array?j(s.array):void 0,s.spill&&o.placeSpill(n,s.spill),s.blocked&&o.blockSpill(n,s.blocked),n.state=0)}this.cycles=t.cycles.flatMap(s=>{const o=s.flatMap(n=>{const i=this.wb.sheets[n.sheet]?.at(n.row,n.col);return i instanceof k?[i]:[]});return o.length>0?[o]:[]});const e=new Map(this.hiddenBy);this.hiddenBy.clear();for(const{sheet:s,rows:o}of t.hidden){const n=this.wb.sheets[s];if(!n)continue;const i=e.get(n),r=i?.size===o.length&&o.every(a=>i.has(a));this.hiddenBy.set(n,r?i:new Set(o))}for(const s of this.wb.sheets)s.tighten()}numberOf(t){return Z(t)}}const at=(l,t)=>l?.top===t?.top&&l?.left===t?.left&&l?.bottom===t?.bottom&&l?.right===t?.right;function ct(l){return l.data.some(t=>t===null)?new m(l.rows,l.cols,l.data.map(t=>t??0)):l}function R(l){return l?.kind==="call"&&l.name==="TABLE"}function F(l){return{sheet:l.sheet,row:l.row,col:l.col,dr:l.row-l.shared.row,dc:l.col-l.shared.col}}function lt(l,t,e){const{sheet:s}=l,o=l.area;let n=o.left,i=o.right;const[r,a]=t.columns;if(r!==void 0){const A=l.column(r),y=a===void 0?A:l.column(a);if(!A||!y)return h.REF;n=o.left+Math.min(A.index,y.index),i=o.left+Math.max(A.index,y.index)}if(t.thisRow)return e.sheet!==s||e.row<=o.top||e.row>o.bottom?h.VALUE:new d(s,e.row,n,e.row,i);const c=t.sections;if(c.includes("#Totals"))return h.REF;const f=c.includes("#All")||c.includes("#Headers"),u=c.length===0||c.includes("#All")||c.includes("#Data"),w=f?o.top:o.top+1,E=u?o.bottom:o.top;return new d(s,w,n,E,i)}function ht(l,t,e){return new S([],{kind:"call",name:l.name,written:t,args:[]},e,l.name)}
@@ -10,6 +10,12 @@ export declare function indexKey(v: Scalar): string | undefined;
10
10
  /** A wildcard pattern as a test: * any text, ? one character, ~ takes the next as it is. */
11
11
  export declare function wildcard(pattern: string): (text: string) => boolean;
12
12
  export declare const hasWildcard: (text: string) => boolean;
13
+ /**
14
+ * The text a pattern matches when it holds no * or ? of its own, only
15
+ * ~-escapes ("~"&A1 in a MATCH): one text, found by an index, not tried
16
+ * against every cell.
17
+ */
18
+ export declare function literalOf(pattern: string): string | undefined;
13
19
  /** A criterion read from what a formula gives it. */
14
20
  export declare function criterion(raw: Scalar): Criterion;
15
21
  //# sourceMappingURL=criteria.d.ts.map
@@ -1,2 +1,2 @@
1
1
  /*! AllyHub. Copyright (c) 2026 AllyHub, Inc. All rights reserved. Use is subject to LICENSE.md. */
2
- import{CellError as l,ERR as I}from"../../values.js";import{compare as R,numberOfText as S,sameNumber as b}from"../coerce.js";import{Missing as T,Reference as A}from"../types.js";import{indexKeyOf as k}from"../indexes.js";import{cellsOf as h,raw as p}from"./define.js";export function indexKey(e){if(typeof e=="number")return`n${String(Number(e.toPrecision(15)))}`;if(typeof e=="string")return`t${e.toUpperCase()}`;if(typeof e=="boolean")return e?"bT":"bF"}export function wildcard(e){let r="";for(let t=0;t<e.length;t++){const o=e.charAt(t);o==="~"&&/[*?~]/u.test(e.charAt(t+1))?(t+=1,r+=e.charAt(t).replace(/[*?]/u,"\\$&")):o==="*"?r+="[\\s\\S]*":o==="?"?r+="[\\s\\S]":r+=o.replace(/[.+^${}()|[\]\\/-]/gu,"\\$&")}const n=new RegExp(`^${r}$`,"iu");return t=>n.test(t)}export const hasWildcard=e=>/[*?~]/u.test(e);function g(e){return r=>{if(typeof r=="number")return b(r,e);if(typeof r!="string")return!1;const n=S(r);return n!==void 0&&b(n,e)}}function w(e,r){const n=t=>e===">"?t>0:e===">="?t>=0:e==="<"?t<0:t<=0;return t=>typeof t==typeof r&&n(R(t,r))}export function criterion(e){if(e===null)return{test:g(0),key:"n0"};if(typeof e=="number")return{test:g(e),key:`n${String(Number(e.toPrecision(15)))}`};if(typeof e=="boolean")return{test:s=>s===e,key:e?"bT":"bF"};if(e instanceof l)return{test:s=>s===e};const r=/^(<>|>=|<=|=|>|<)/u.exec(e),n=r?.[1]??"=",t=r?e.slice(n.length):e,o=s=>s===null;if(t==="")return n==="="?{test:r?o:s=>s===null||s===""}:n==="<>"?{test:s=>s!==null}:{test:()=>!1};const i=S(t),c=/^(?:TRUE|FALSE)$/iu.test(t)?t.toUpperCase()==="TRUE":void 0,f=i??c;if(n==="="||n==="<>"){let s,u;if(i!==void 0)s=g(i),u=indexKey(i);else if(c!==void 0)s=d=>d===c,u=indexKey(c);else if(t.startsWith("#"))s=d=>d instanceof l&&d.code===t.toUpperCase();else if(hasWildcard(t)){const d=wildcard(t);s=m=>typeof m=="string"&&d(m)}else{const d=t.toUpperCase();s=m=>typeof m=="string"&&m.toUpperCase()===d,u=indexKey(t)}return n==="="?u===void 0?{test:s}:{test:s,key:u}:{test:d=>!s(d)}}return{test:w(n,f??t)}}function O(e,r){const n=e.reference;if(!(n?.cols!==1||e.rows<64))return r.calc.indexes.of(k("crit",n),[n],()=>{const t=new Map,o=(i,c)=>{if(i===void 0)return;const f=t.get(i);f?f.push(c):t.set(i,[c])};for(let i=0;i<e.rows;i++){const c=e.at(i,0);if(o(indexKey(c),i),typeof c=="string"){const f=S(c);f!==void 0&&o(indexKey(f),i)}}return t})}function F(e,r){const n=r.calc.scalar(e,r.frame);return criterion(n)}function $(e,r){const n=[];for(let o=0;o+1<e.length;o+=2){const i=h(e[o],r);if(i instanceof l)return i;const c=F(e[o+1],r);if(c instanceof l)return c;n.push({cells:i,crit:c})}const t=n[0]?.cells;return t&&n.some(o=>!x(o.cells,t))?I.VALUE:n}const x=(e,r)=>e.cols===r.cols&&(e.reference?.rows??e.rows)===(r.reference?.rows??r.rows);function E(e,r,n){const t=e[0];if(!t)return;let o,i;for(const f of e){if(f.crit.key===void 0)continue;const s=O(f.cells,r);if(!s)continue;const u=s.get(f.crit.key)??[];(o===void 0||u.length<o.length)&&(o=u,i=f)}if(o!==void 0){const f=e.filter(s=>s!==i);for(const s of o)f.every(u=>u.crit.test(u.cells.at(s,0)))&&n(s,0);return}const c=Math.max(...e.map(f=>f.cells.rows));for(let f=0;f<c;f++)for(let s=0;s<t.cells.cols;s++)e.every(u=>u.crit.test(u.cells.at(f,s)))&&n(f,s)}function V(e,r){return e instanceof A?(e.rows-r.rows)*e.cols:0}function M(e,r,n,t){let o;return E(r,n,(i,c)=>{if(o)return;const f=e.at(i,c);f instanceof l?o=f:typeof f=="number"&&t(f)}),o}function G(e,r){const n=h(e[0],r);if(n instanceof l)return n;const t=e[2];return t===void 0?n:h(t,r)}const C=e=>(r,n)=>{const t=h(r[0],n);if(t instanceof l)return t;const o=F(r[1],n);if(o instanceof l)return o;const i=G(r,n);if(i instanceof l)return i;const c=[];return M(i,[{cells:t,crit:o}],n,s=>{c.push(s)})??e(c)},y=e=>(r,n)=>{const t=h(r[0],n);if(t instanceof l)return t;const o=$(r.slice(1),n);if(o instanceof l)return o;const i=o[0]?.cells;if(i&&!x(t,i))return I.VALUE;const c=[];return M(t,o,n,s=>{c.push(s)})??e(c)},a=(e,r)=>(n,t)=>{const o=[],i=[e];for(const u of n)if(u instanceof A)o.push(u),i.push(`r${u.key}`);else if(u instanceof T)i.push("-");else if(u instanceof l)i.push(`e${u.code}`);else if(u===null||typeof u!="object")i.push(`${typeof u}:${String(u)}`);else return r(n,t);if(o.length===0)return r(n,t);const c=i.join("\0"),f=t.calc.results.get(c);if(f!==void 0)return f;const s=r(n,t);return(s===null||typeof s!="object"||s instanceof l)&&t.calc.results.set(c,o,s),s},U=e=>e.reduce((r,n)=>r+n,0),N=e=>e.length===0?I.DIV0:U(e)/e.length,j=e=>e.length===0?0:e.reduce((r,n)=>n<r?n:r),v=e=>e.length===0?0:e.reduce((r,n)=>n>r?n:r);p("SUMIF","rv?r",a("SUMIF",C(U))),p("AVERAGEIF","rv?r",a("AVERAGEIF",C(N))),p("SUMIFS","r(rv)",a("SUMIFS",y(U))),p("AVERAGEIFS","r(rv)",a("AVERAGEIFS",y(N))),p("MINIFS","r(rv)",a("MINIFS",y(j))),p("MAXIFS","r(rv)",a("MAXIFS",y(v))),p("COUNTIF","rv",a("COUNTIF",(e,r)=>{const n=h(e[0],r);if(n instanceof l)return n;const t=F(e[1],r);if(t instanceof l)return t;let o=0;return E([{cells:n,crit:t}],r,()=>{o+=1}),o+(t.test(null)?V(e[0],n):0)})),p("COUNTIFS","(rv)",a("COUNTIFS",(e,r)=>{const n=$(e,r);if(n instanceof l)return n;let t=0;return E(n,r,()=>{t+=1}),t}));
2
+ import{CellError as l,ERR as g}from"../../values.js";import{compare as O,numberOfText as A,sameNumber as b}from"../coerce.js";import{Missing as C,Reference as T}from"../types.js";import{indexKeyOf as w}from"../indexes.js";import{cellsOf as h,raw as p}from"./define.js";export function indexKey(e){if(typeof e=="number")return`n${String(Number(e.toPrecision(15)))}`;if(typeof e=="string")return`t${e.toUpperCase()}`;if(typeof e=="boolean")return e?"bT":"bF"}const y=new Map,k=1e4;export function wildcard(e){const n=y.get(e);if(n)return n;y.size>=k&&y.clear();const t=V(e);return y.set(e,t),t}function V(e){let n="";for(let r=0;r<e.length;r++){const o=e.charAt(r);o==="~"&&/[*?~]/u.test(e.charAt(r+1))?(r+=1,n+=e.charAt(r).replace(/[*?]/u,"\\$&")):o==="*"?n+="[\\s\\S]*":o==="?"?n+="[\\s\\S]":n+=o.replace(/[.*+?^${}()|[\]\\]/gu,"\\$&")}const t=new RegExp(`^${n}$`,"iu");return r=>t.test(r)}export const hasWildcard=e=>/[*?~]/u.test(e);export function literalOf(e){let n="";for(let t=0;t<e.length;t++){const r=e.charAt(t);if(r==="~"&&/[*?~]/u.test(e.charAt(t+1)))t+=1,n+=e.charAt(t);else{if(r==="*"||r==="?")return;n+=r}}return n}function I(e){return n=>{if(typeof n=="number")return b(n,e);if(typeof n!="string")return!1;const t=A(n);return t!==void 0&&b(t,e)}}function G(e,n){const t=r=>e===">"?r>0:e===">="?r>=0:e==="<"?r<0:r<=0;return r=>typeof r==typeof n&&t(O(r,n))}export function criterion(e){if(e===null)return{test:I(0),key:"n0"};if(typeof e=="number")return{test:I(e),key:`n${String(Number(e.toPrecision(15)))}`};if(typeof e=="boolean")return{test:s=>s===e,key:e?"bT":"bF"};if(e instanceof l)return{test:s=>s===e};const n=/^(<>|>=|<=|=|>|<)/u.exec(e),t=n?.[1]??"=",r=n?e.slice(t.length):e,o=s=>s===null;if(r==="")return t==="="?{test:n?o:s=>s===null||s===""}:t==="<>"?{test:s=>s!==null}:{test:()=>!1};const i=A(r),c=/^(?:TRUE|FALSE)$/iu.test(r)?r.toUpperCase()==="TRUE":void 0,f=i??c;if(t==="="||t==="<>"){let s,u;if(i!==void 0)s=I(i),u=indexKey(i);else if(c!==void 0)s=d=>d===c,u=indexKey(c);else if(r.startsWith("#"))s=d=>d instanceof l&&d.code===r.toUpperCase();else if(hasWildcard(r)){const d=wildcard(r);s=m=>typeof m=="string"&&d(m)}else{const d=r.toUpperCase();s=m=>typeof m=="string"&&m.toUpperCase()===d,u=indexKey(r)}return t==="="?u===void 0?{test:s}:{test:s,key:u}:{test:d=>!s(d)}}return{test:G(t,f??r)}}function P(e,n){const t=e.reference;if(!(t?.cols!==1||e.rows<64))return n.calc.indexes.ofRepeated(w("crit",t),[t],()=>{const r=new Map,o=(i,c)=>{if(i===void 0)return;const f=r.get(i);f?f.push(c):r.set(i,[c])};for(let i=0;i<e.rows;i++){const c=e.at(i,0);if(o(indexKey(c),i),typeof c=="string"){const f=A(c);f!==void 0&&o(indexKey(f),i)}}return r})}function E(e,n){const t=n.calc.scalar(e,n.frame);return criterion(t)}function x(e,n){const t=[];for(let o=0;o+1<e.length;o+=2){const i=h(e[o],n);if(i instanceof l)return i;const c=E(e[o+1],n);if(c instanceof l)return c;t.push({cells:i,crit:c})}const r=t[0]?.cells;return r&&t.some(o=>!M(o.cells,r))?g.VALUE:t}const M=(e,n)=>e.cols===n.cols&&(e.reference?.rows??e.rows)===(n.reference?.rows??n.rows);function F(e,n,t){const r=e[0];if(!r)return;let o,i;for(const f of e){if(f.crit.key===void 0)continue;const s=P(f.cells,n);if(!s)continue;const u=s.get(f.crit.key)??[];(o===void 0||u.length<o.length)&&(o=u,i=f)}if(o!==void 0){const f=e.filter(s=>s!==i);for(const s of o)f.every(u=>u.crit.test(u.cells.at(s,0)))&&t(s,0);return}const c=Math.max(...e.map(f=>f.cells.rows));for(let f=0;f<c;f++)for(let s=0;s<r.cells.cols;s++)e.every(u=>u.crit.test(u.cells.at(f,s)))&&t(f,s)}function j(e,n){return e instanceof T?(e.rows-n.rows)*e.cols:0}function R(e,n,t,r){let o;return F(n,t,(i,c)=>{if(o)return;const f=e.at(i,c);f instanceof l?o=f:typeof f=="number"&&r(f)}),o}function v(e,n){const t=h(e[0],n);if(t instanceof l)return t;const r=e[2];return r===void 0?t:h(r,n)}const $=e=>(n,t)=>{const r=h(n[0],t);if(r instanceof l)return r;const o=E(n[1],t);if(o instanceof l)return o;const i=v(n,t);if(i instanceof l)return i;const c=[];return R(i,[{cells:r,crit:o}],t,s=>{c.push(s)})??e(c)},S=e=>(n,t)=>{const r=h(n[0],t);if(r instanceof l)return r;const o=x(n.slice(1),t);if(o instanceof l)return o;const i=o[0]?.cells;if(i&&!M(r,i))return g.VALUE;const c=[];return R(r,o,t,s=>{c.push(s)})??e(c)},a=(e,n)=>(t,r)=>{const o=[],i=[e];for(const u of t)if(u instanceof T)o.push(u),i.push(`r${u.key}`);else if(u instanceof C)i.push("-");else if(u instanceof l)i.push(`e${u.code}`);else if(u===null||typeof u!="object")i.push(`${typeof u}:${String(u)}`);else return n(t,r);if(o.length===0)return n(t,r);const c=i.join("\0"),f=r.calc.results.get(c);if(f!==void 0)return f;const s=n(t,r);return(s===null||typeof s!="object"||s instanceof l)&&r.calc.results.set(c,o,s),s},U=e=>e.reduce((n,t)=>n+t,0),N=e=>e.length===0?g.DIV0:U(e)/e.length,L=e=>e.length===0?0:e.reduce((n,t)=>t<n?t:n),z=e=>e.length===0?0:e.reduce((n,t)=>t>n?t:n);p("SUMIF","rv?r",a("SUMIF",$(U))),p("AVERAGEIF","rv?r",a("AVERAGEIF",$(N))),p("SUMIFS","r(rv)",a("SUMIFS",S(U))),p("AVERAGEIFS","r(rv)",a("AVERAGEIFS",S(N))),p("MINIFS","r(rv)",a("MINIFS",S(L))),p("MAXIFS","r(rv)",a("MAXIFS",S(z))),p("COUNTIF","rv",a("COUNTIF",(e,n)=>{const t=h(e[0],n);if(t instanceof l)return t;const r=E(e[1],n);if(r instanceof l)return r;let o=0;return F([{cells:t,crit:r}],n,()=>{o+=1}),o+(r.test(null)?j(e[0],t):0)})),p("COUNTIFS","(rv)",a("COUNTIFS",(e,n)=>{const t=x(e,n);if(t instanceof l)return t;let r=0;return F(t,n,()=>{r+=1}),r}));
@@ -1,2 +1,2 @@
1
1
  /*! AllyHub. Copyright (c) 2026 AllyHub, Inc. All rights reserved. Use is subject to LICENSE.md. */
2
- import{parseDate as B,parseTime as H,parseValue as S}from"numfmt";import{CellError as c,ERR as i,dateOf as h,serialOf as U}from"../../values.js";import{toBool as X,toNumber as m,toText as T}from"../coerce.js";import{Missing as O}from"../types.js";import{number as I,numbersOf as Y,raw as p,scalar as l}from"./define.js";const j=2958465,d=r=>{const n=m(r);return n instanceof c?n:n<0||n>=j+1?i.NUM:Math.floor(n)},b=r=>r%4===0&&r%100!==0||r%400===0,A=(r,n)=>n===2?b(r)?29:28:[4,6,9,11].includes(n)?30:31,V=r=>r.month===2&&r.day===A(r.year,2);l("DATE",3,3,I((r=0,n=1,e=1)=>{const t=Math.trunc(r);if(t<0||t>9999)return i.NUM;const o=U(t<1900?t+1900:t,Math.trunc(n),Math.trunc(e));return o<0?i.NUM:o})),l("TIME",3,3,I((r=0,n=0,e=0)=>{const t=Math.trunc(r)*3600+Math.trunc(n)*60+Math.trunc(e);return t<0?i.NUM:t%86400/86400})),l("YEAR",1,1,r=>{const n=d(r);return n instanceof c?n:h(n).year}),l("MONTH",1,1,r=>{const n=d(r);return n instanceof c?n:h(n).month}),l("DAY",1,1,r=>{const n=d(r);return n instanceof c?n:h(n).day});const R=r=>{const n=m(r);return n instanceof c?n:n<0?i.NUM:Math.round((n-Math.floor(n))*86400)%86400};l("HOUR",1,1,r=>{const n=R(r);return n instanceof c?n:Math.floor(n/3600)}),l("MINUTE",1,1,r=>{const n=R(r);return n instanceof c?n:Math.floor(n/60)%60}),l("SECOND",1,1,r=>{const n=R(r);return n instanceof c?n:n%60}),l("DATEVALUE",1,1,r=>{const n=T(r);if(n instanceof c)return n;const e=B(n)??S(n);return typeof e?.value=="number"?Math.floor(e.value):i.VALUE}),l("TIMEVALUE",1,1,r=>{const n=T(r);if(n instanceof c)return n;const e=H(n)??S(n);return typeof e?.value!="number"?i.VALUE:e.value-Math.floor(e.value)});const D=r=>(r%7+6)%7;l("WEEKDAY",1,2,(r,n)=>{const e=d(r);if(e instanceof c)return e;const t=n==null?1:m(n);if(t instanceof c)return t;const o=D(e);return t===1?o+1:t===2?(o+6)%7+1:t===3?(o+6)%7:t>=11&&t<=17?(o+7-(t-10)%7)%7+1:i.NUM});const W=r=>{const n=(D(r)+6)%7,e=r-n+3,{year:t}=h(e);return Math.floor((e-U(t,1,1))/7)+1};l("ISOWEEKNUM",1,1,r=>{const n=d(r);return n instanceof c?n:W(n)}),l("WEEKNUM",1,2,(r,n)=>{const e=d(r);if(e instanceof c)return e;const t=n==null?1:m(n);if(t instanceof c)return t;if(t===21)return W(e);const o=t===1?0:t===2?1:t>=11&&t<=17?(t-10)%7:-1;if(o<0)return i.NUM;const a=U(h(e).year,1,1),s=(D(a)+7-o)%7;return Math.floor((e-a+s)/7)+1});const L=(r,n)=>{const{year:e,month:t,day:o}=h(r),a=e*12+t-1+n,s=Math.floor(a/12),f=a-s*12+1;return U(s,f,Math.min(o,A(s,f)))};l("EDATE",2,2,(r,n)=>{const e=d(r),t=m(n);if(e instanceof c)return e;if(t instanceof c)return t;const o=L(e,Math.trunc(t));return o<0?i.NUM:o}),l("EOMONTH",2,2,(r,n)=>{const e=d(r),t=m(n);if(e instanceof c)return e;if(t instanceof c)return t;const o=h(L(e,Math.trunc(t))),a=U(o.year,o.month,A(o.year,o.month));return a<0?i.NUM:a}),l("DAYS",2,2,(r,n)=>{const e=d(r),t=d(n);return e instanceof c?e:t instanceof c?t:e-t}),l("DATEDIF",3,3,(r,n,e)=>{const t=d(r),o=d(n),a=T(e);if(t instanceof c)return t;if(o instanceof c)return o;if(a instanceof c)return a;if(t>o)return i.NUM;const s=h(t),f=h(o),u=(f.year-s.year)*12+f.month-s.month-(f.day<s.day?1:0);switch(a.toUpperCase()){case"D":return o-t;case"M":return u;case"Y":return Math.floor(u/12);case"YM":return u%12;case"MD":{if(f.day>=s.day)return f.day-s.day;const y=f.month===1?12:f.month-1,N=f.month===1?f.year-1:f.year;return A(N,y)-s.day+f.day}case"YD":{const y=f.year-s.year-(u%12<0?1:0),N=Math.floor(u/12),M=L(t,N*12);return y>=0?o-M:i.NUM}default:return i.NUM}});function _(r,n,e){const t=h(r),o=h(n);let a=t.day,s=o.day,f=o.month,u=o.year;return e?(a=Math.min(a,30),s=Math.min(s,30)):(a===A(t.year,t.month)&&(a=30),s===31&&(a<30?(s=1,f+=1,f>12&&(f=1,u+=1)):s=30)),(u-t.year)*360+(f-t.month)*30+s-a}l("DAYS360",2,3,(r,n,e)=>{const t=d(r),o=d(n),a=e===void 0?!1:X(e);return t instanceof c?t:o instanceof c?o:a instanceof c?a:_(t,o,a)});export function yearFrac(r,n,e){const[t,o]=r<=n?[r,n]:[n,r],a=h(t),s=h(o),f=o-t;switch(e){case 0:{let u=a.day,y=s.day;return V(a)&&(V(s)&&(y=30),u=30),y===31&&u>=30&&(y=30),u===31&&(u=30),((s.year-a.year)*360+(s.month-a.month)*30+y-u)/360}case 2:return f/360;case 3:return f/365;case 4:return _(t,o,!0)/360;default:{if(a.year===s.year||s.year===a.year+1&&(a.month>s.month||a.month===s.month&&a.day>=s.day)){let M;if(a.year===s.year)M=b(a.year);else{const E=w=>b(w)?U(w,2,29):1/0;M=E(a.year)>=t&&E(a.year)<=o||E(s.year)>=t&&E(s.year)<=o}return f/(M?366:365)}const y=s.year-a.year+1,N=U(s.year+1,1,1)-U(a.year,1,1);return f/(N/y)}}}l("YEARFRAC",2,3,(r,n,e)=>{const t=d(r),o=d(n),a=e==null?0:m(e);if(t instanceof c)return t;if(o instanceof c)return o;if(a instanceof c)return a;const s=Math.trunc(a);return s<0||s>4?i.NUM:yearFrac(t,o,s)});function k(r){const n=(...t)=>[0,1,2,3,4,5,6].map(o=>t.includes(o));if(r==null)return n(0,6);if(typeof r=="string"){if(!/^[01]{7}$/u.test(r)||r==="1111111")return i.VALUE;const t=r.split("").map(o=>o==="1");return[t[6]??!1,...t.slice(0,6)]}const e=m(r);return e instanceof c?e:e>=1&&e<=7?n((e+5)%7,(e+6)%7):e>=11&&e<=17?n(e-11):i.NUM}function K(r,n){if(r===void 0||r instanceof O)return new Set;const e=Y([r],n);return e instanceof c?e:new Set(e.map(t=>Math.floor(t)))}function P(r,n,e,t){const o=r<=n?1:-1;let a=0;for(let s=r;o>0?s<=n:s>=n;s+=o)e[D(s)]!==!0&&!t.has(s)&&(a+=o);return a}function $(r,n,e,t){if(e.every(Boolean))return i.VALUE;const o=n<0?-1:1;let a=r;for(let s=Math.abs(n);s>0;)a+=o,e[D(a)]!==!0&&!t.has(a)&&(s-=1);return a<0?i.NUM:a}const x=r=>(n,e)=>{const t=d(e.calc.scalar(n[0],e.frame)),o=d(e.calc.scalar(n[1],e.frame));if(t instanceof c)return t;if(o instanceof c)return o;const a=r?n[2]:void 0,s=k(a===void 0||a instanceof O?void 0:e.calc.scalar(a,e.frame));if(s instanceof c)return s;const f=K(n[r?3:2],e);return f instanceof c?f:P(t,o,s,f)},F=r=>(n,e)=>{const t=d(e.calc.scalar(n[0],e.frame)),o=m(e.calc.scalar(n[1],e.frame));if(t instanceof c)return t;if(o instanceof c)return o;const a=r?n[2]:void 0,s=k(a===void 0||a instanceof O?void 0:e.calc.scalar(a,e.frame));if(s instanceof c)return s;const f=K(n[r?3:2],e);return f instanceof c?f:$(t,Math.trunc(o),s,f)};p("NETWORKDAYS","vv?r",x(!1)),p("NETWORKDAYS.INTL","vv?vr",x(!0)),p("WORKDAY","vv?r",F(!1)),p("WORKDAY.INTL","vv?vr",F(!0)),p("NOW","",(r,n)=>n.calc.nowSerial(),{volatile:!0}),p("TODAY","",(r,n)=>Math.floor(n.calc.nowSerial()),{volatile:!0});function g(r,n,e){const t=e[0]??0;let o=0;return n.forEach((a,s)=>{o+=a/(1+r)**(((e[s]??t)-t)/365)}),o}const C=(r,n)=>{const e=Y([r[0]],n);if(e instanceof c)return e;const t=Y([r[1]],n);return t instanceof c?t:e.length!==t.length||e.length===0?i.NUM:{values:e,dates:t.map(Math.floor)}};p("XNPV","vrr",(r,n)=>{const e=m(n.calc.scalar(r[0],n.frame));if(e instanceof c)return e;const t=C(r.slice(1),n);return t instanceof c?t:e<=0?i.NUM:g(e,t.values,t.dates)}),p("XIRR","rr?v",(r,n)=>{const e=C(r,n);if(e instanceof c)return e;const{values:t,dates:o}=e;if(!t.some(u=>u>0)||!t.some(u=>u<0))return i.NUM;const a=r[2]===void 0||r[2]instanceof O?.1:m(n.calc.scalar(r[2],n.frame));if(a instanceof c)return a;const s=o[0]??0;let f=a;for(let u=0;u<100;u++){let y=0,N=0;t.forEach((E,w)=>{const v=((o[w]??s)-s)/365;y+=E/(1+f)**v,N-=v*E/(1+f)**(v+1)});const M=f-y/N;if(!Number.isFinite(M))return i.NUM;if(Math.abs(M-f)<1e-10)return M;f=M<=-1?(f-1)/2:M}return i.NUM});
2
+ import{parseDate as B,parseTime as H,parseValue as L}from"numfmt";import{CellError as c,ERR as u,dateOf as h,serialOf as p}from"../../values.js";import{toBool as X,toNumber as m,toText as T}from"../coerce.js";import{Missing as v}from"../types.js";import{eachRow as g,number as S,numbersOf as j,raw as U,scalar as l}from"./define.js";const P=2958465,y=e=>{const n=m(e);return n instanceof c?n:n<0||n>=P+1?u.NUM:Math.floor(n)},Y=e=>e%4===0&&e%100!==0||e%400===0,A=(e,n)=>n===2?Y(e)?29:28:[4,6,9,11].includes(n)?30:31,I=e=>e.month===2&&e.day===A(e.year,2);l("DATE",3,3,S((e=0,n=1,r=1)=>{const t=Math.trunc(e);if(t<0||t>9999)return u.NUM;const o=p(t<1900?t+1900:t,Math.trunc(n),Math.trunc(r));return o<0?u.NUM:o})),l("TIME",3,3,S((e=0,n=0,r=0)=>{const t=Math.trunc(e)*3600+Math.trunc(n)*60+Math.trunc(r);return t<0?u.NUM:t%86400/86400})),l("YEAR",1,1,e=>{const n=y(e);return n instanceof c?n:h(n).year}),l("MONTH",1,1,e=>{const n=y(e);return n instanceof c?n:h(n).month}),l("DAY",1,1,e=>{const n=y(e);return n instanceof c?n:h(n).day});const b=e=>{const n=m(e);return n instanceof c?n:n<0?u.NUM:Math.round((n-Math.floor(n))*86400)%86400};l("HOUR",1,1,e=>{const n=b(e);return n instanceof c?n:Math.floor(n/3600)}),l("MINUTE",1,1,e=>{const n=b(e);return n instanceof c?n:Math.floor(n/60)%60}),l("SECOND",1,1,e=>{const n=b(e);return n instanceof c?n:n%60}),l("DATEVALUE",1,1,e=>{const n=T(e);if(n instanceof c)return n;const r=B(n)??L(n);return typeof r?.value=="number"?Math.floor(r.value):u.VALUE}),l("TIMEVALUE",1,1,e=>{const n=T(e);if(n instanceof c)return n;const r=H(n)??L(n);return typeof r?.value!="number"?u.VALUE:r.value-Math.floor(r.value)});const D=e=>(e%7+6)%7;l("WEEKDAY",1,2,(e,n)=>{const r=y(e);if(r instanceof c)return r;const t=n==null?1:m(n);if(t instanceof c)return t;const o=D(r);return t===1?o+1:t===2?(o+6)%7+1:t===3?(o+6)%7:t>=11&&t<=17?(o+7-(t-10)%7)%7+1:u.NUM});const V=e=>{const n=(D(e)+6)%7,r=e-n+3,{year:t}=h(r);return Math.floor((r-p(t,1,1))/7)+1};l("ISOWEEKNUM",1,1,e=>{const n=y(e);return n instanceof c?n:V(n)}),l("WEEKNUM",1,2,(e,n)=>{const r=y(e);if(r instanceof c)return r;const t=n==null?1:m(n);if(t instanceof c)return t;if(t===21)return V(r);const o=t===1?0:t===2?1:t>=11&&t<=17?(t-10)%7:-1;if(o<0)return u.NUM;const a=p(h(r).year,1,1),s=(D(a)+7-o)%7;return Math.floor((r-a+s)/7)+1});const R=(e,n)=>{const{year:r,month:t,day:o}=h(e),a=r*12+t-1+n,s=Math.floor(a/12),f=a-s*12+1;return p(s,f,Math.min(o,A(s,f)))};l("EDATE",2,2,(e,n)=>{const r=y(e),t=m(n);if(r instanceof c)return r;if(t instanceof c)return t;const o=R(r,Math.trunc(t));return o<0?u.NUM:o}),l("EOMONTH",2,2,(e,n)=>{const r=y(e),t=m(n);if(r instanceof c)return r;if(t instanceof c)return t;const o=h(R(r,Math.trunc(t))),a=p(o.year,o.month,A(o.year,o.month));return a<0?u.NUM:a}),l("DAYS",2,2,(e,n)=>{const r=y(e),t=y(n);return r instanceof c?r:t instanceof c?t:r-t}),l("DATEDIF",3,3,(e,n,r)=>{const t=y(e),o=y(n),a=T(r);if(t instanceof c)return t;if(o instanceof c)return o;if(a instanceof c)return a;if(t>o)return u.NUM;const s=h(t),f=h(o),i=(f.year-s.year)*12+f.month-s.month-(f.day<s.day?1:0);switch(a.toUpperCase()){case"D":return o-t;case"M":return i;case"Y":return Math.floor(i/12);case"YM":return i%12;case"MD":{if(f.day>=s.day)return f.day-s.day;const d=f.month===1?12:f.month-1,N=f.month===1?f.year-1:f.year;return A(N,d)-s.day+f.day}case"YD":{const d=f.year-s.year-(i%12<0?1:0),N=Math.floor(i/12),M=R(t,N*12);return d>=0?o-M:u.NUM}default:return u.NUM}});function W(e,n,r){const t=h(e),o=h(n);let a=t.day,s=o.day,f=o.month,i=o.year;return r?(a=Math.min(a,30),s=Math.min(s,30)):(a===A(t.year,t.month)&&(a=30),s===31&&(a<30?(s=1,f+=1,f>12&&(f=1,i+=1)):s=30)),(i-t.year)*360+(f-t.month)*30+s-a}l("DAYS360",2,3,(e,n,r)=>{const t=y(e),o=y(n),a=r===void 0?!1:X(r);return t instanceof c?t:o instanceof c?o:a instanceof c?a:W(t,o,a)});export function yearFrac(e,n,r){const[t,o]=e<=n?[e,n]:[n,e],a=h(t),s=h(o),f=o-t;switch(r){case 0:{let i=a.day,d=s.day;return I(a)&&(I(s)&&(d=30),i=30),d===31&&i>=30&&(d=30),i===31&&(i=30),((s.year-a.year)*360+(s.month-a.month)*30+d-i)/360}case 2:return f/360;case 3:return f/365;case 4:return W(t,o,!0)/360;default:{if(a.year===s.year||s.year===a.year+1&&(a.month>s.month||a.month===s.month&&a.day>=s.day)){let M;if(a.year===s.year)M=Y(a.year);else{const E=w=>Y(w)?p(w,2,29):1/0;M=E(a.year)>=t&&E(a.year)<=o||E(s.year)>=t&&E(s.year)<=o}return f/(M?366:365)}const d=s.year-a.year+1,N=p(s.year+1,1,1)-p(a.year,1,1);return f/(N/d)}}}l("YEARFRAC",2,3,(e,n,r)=>{const t=y(e),o=y(n),a=r==null?0:m(r);if(t instanceof c)return t;if(o instanceof c)return o;if(a instanceof c)return a;const s=Math.trunc(a);return s<0||s>4?u.NUM:yearFrac(t,o,s)});function _(e){const n=(...t)=>[0,1,2,3,4,5,6].map(o=>t.includes(o));if(e==null)return n(0,6);if(typeof e=="string"){if(!/^[01]{7}$/u.test(e)||e==="1111111")return u.VALUE;const t=e.split("").map(o=>o==="1");return[t[6]??!1,...t.slice(0,6)]}const r=m(e);return r instanceof c?r:r>=1&&r<=7?n((r+5)%7,(r+6)%7):r>=11&&r<=17?n(r-11):u.NUM}function k(e,n){if(e===void 0||e instanceof v)return new Set;const r=j([e],n);return r instanceof c?r:new Set(r.map(t=>Math.floor(t)))}function $(e,n,r,t){const o=e<=n?1:-1;let a=0;for(let s=e;o>0?s<=n:s>=n;s+=o)r[D(s)]!==!0&&!t.has(s)&&(a+=o);return a}function q(e,n,r,t){if(r.every(Boolean))return u.VALUE;const o=n<0?-1:1;let a=e;for(let s=Math.abs(n);s>0;)a+=o,r[D(a)]!==!0&&!t.has(a)&&(s-=1);return a<0?u.NUM:a}const K=e=>(n,r)=>{const t=y(r.calc.scalar(n[0],r.frame)),o=y(r.calc.scalar(n[1],r.frame));if(t instanceof c)return t;if(o instanceof c)return o;const a=e?n[2]:void 0,s=_(a===void 0||a instanceof v?void 0:r.calc.scalar(a,r.frame));if(s instanceof c)return s;const f=k(n[e?3:2],r);return f instanceof c?f:$(t,o,s,f)},x=e=>(n,r)=>{const t=y(r.calc.scalar(n[0],r.frame)),o=m(r.calc.scalar(n[1],r.frame));if(t instanceof c)return t;if(o instanceof c)return o;const a=e?n[2]:void 0,s=_(a===void 0||a instanceof v?void 0:r.calc.scalar(a,r.frame));if(s instanceof c)return s;const f=k(n[e?3:2],r);return f instanceof c?f:q(t,Math.trunc(o),s,f)};U("NETWORKDAYS","vv?r",K(!1)),U("NETWORKDAYS.INTL","vv?vr",K(!0)),U("WORKDAY","vv?r",x(!1)),U("WORKDAY.INTL","vv?vr",x(!0)),U("NOW","",(e,n)=>n.calc.nowSerial(),{volatile:!0}),U("TODAY","",(e,n)=>Math.floor(n.calc.nowSerial()),{volatile:!0});function z(e,n,r){const t=r[0]??0;let o=0;return n.forEach((a,s)=>{o+=a/(1+e)**(((r[s]??t)-t)/365)}),o}const F=(e,n)=>{const r=[];return g([e],n,t=>r.push(t)),r},C=(e,n)=>{const r=F(e[0],n),t=F(e[1],n);if(r.length!==t.length)return u.NUM;const o=[],a=[];for(const[s,f]of r.entries()){const i=t[s]??null;if(f instanceof c)return f;if(i instanceof c)return i;if(typeof f=="number"){if(typeof i!="number")return u.VALUE;o.push(f),a.push(Math.floor(i))}}return o.length===0?u.NUM:{values:o,dates:a}};U("XNPV","vrr",(e,n)=>{const r=m(n.calc.scalar(e[0],n.frame));if(r instanceof c)return r;const t=C(e.slice(1),n);return t instanceof c?t:r<=0?u.NUM:z(r,t.values,t.dates)}),U("XIRR","rr?v",(e,n)=>{const r=C(e,n);if(r instanceof c)return r;const{values:t,dates:o}=r;if(!t.some(i=>i>0)||!t.some(i=>i<0))return u.NUM;const a=e[2]===void 0||e[2]instanceof v?.1:m(n.calc.scalar(e[2],n.frame));if(a instanceof c)return a;const s=o[0]??0;let f=a;for(let i=0;i<100;i++){let d=0,N=0;t.forEach((E,w)=>{const O=((o[w]??s)-s)/365;d+=E/(1+f)**O,N-=O*E/(1+f)**(O+1)});const M=f-d/N;if(!Number.isFinite(M))return u.NUM;if(Math.abs(M-f)<1e-10)return M;f=M<=-1?(f-1)/2:M}return u.NUM});
@@ -1,5 +1,5 @@
1
1
  import type { Node } from "../../formula/ast.js";
2
- import { CellError, type Scalar } from "../../values.js";
2
+ import { CellError, Matrix, type Scalar } from "../../values.js";
3
3
  import type { Calc } from "../engine.js";
4
4
  import { Reference, type Frame, type Value } from "../types.js";
5
5
  export interface Call {
@@ -75,6 +75,8 @@ export interface Cells {
75
75
  at: (row: number, col: number) => Scalar;
76
76
  /** Its reference, when it is one. */
77
77
  reference?: Reference;
78
+ /** Its array, when it is one. */
79
+ array?: Matrix;
78
80
  }
79
81
  /** A range's cells, down to the last row its sheet uses. */
80
82
  export declare function cellsOf(v: Value, c: Call): Cells | CellError;
@@ -1,2 +1,2 @@
1
1
  /*! AllyHub. Copyright (c) 2026 AllyHub, Inc. All rights reserved. Use is subject to LICENSE.md. */
2
- import{CellError as l,ERR as a,Matrix as i}from"../../values.js";import{toBool as w,toNumber as c,toText as A}from"../coerce.js";import{Lambda as h,Missing as p,Reference as g,Union as x}from"../types.js";export const FUNCTIONS=new Map;const d=255;export const many=d;export function signature(e){const r=/^([vr?]*)(?:\(([vr]+)\))?$/u.exec(e),t=r?.[1];if(t===void 0||(t.match(/\?/gu)??[]).length>1)throw new Error(`"${e}" is not a signature.`);const o=m=>Array.from({length:m.length},(y,b)=>m.charAt(b)==="v"?"value":"whole"),n=o(t.replace("?","")),f=o(r?.[2]??""),s=t.indexOf("?"),u=s>=0?s:n.length+f.length;return{fixed:n,group:f,min:u,max:f.length>0?d:n.length}}export function paramAt(e,r){return r<e.fixed.length?e.fixed[r]??"whole":e.group.length===0?"whole":e.group[(r-e.fixed.length)%e.group.length]??"whole"}export function fillsGroup(e,r){return e.group.length===0||r<=e.fixed.length?!0:(r-e.fixed.length)%e.group.length===0}export function scalar(e,r,t,o,n={}){FUNCTIONS.set(e,{name:e,min:r,max:t,kind:"scalar",call:o,from:n.from??"allyhub",...n.errors===!0?{errors:!0}:{},...n.volatile===!0?{volatile:!0}:{}})}export function raw(e,r,t,o={}){const{min:n,max:f,fixed:s,group:u}=signature(r);FUNCTIONS.set(e,{name:e,min:n,max:f,kind:"raw",params:{fixed:s,group:u},call:t,from:o.from??"allyhub",...o.volatile===!0?{volatile:!0}:{}})}export function lazy(e,r,t,o){FUNCTIONS.set(e,{name:e,min:r,max:t,kind:"lazy",call:o,from:"allyhub"})}export function alias(e,r){const t=FUNCTIONS.get(r);t&&FUNCTIONS.set(e,{...t,name:e})}export function cellsOf(e,r){if(e instanceof l)return e;if(e instanceof x)return a.VALUE;if(e instanceof g){const o=Math.min(e.bottom,e.sheet.lastRow);return{rows:Math.max(0,o-e.top+1),cols:e.cols,at:(n,f)=>r.calc.read(e.sheet,e.top+n,e.left+f),reference:e}}if(e instanceof i)return{rows:e.rows,cols:e.cols,at:(o,n)=>e.at(o,n)};if(e instanceof h)return a.VALUE;const t=e instanceof p?null:e;return{rows:1,cols:1,at:()=>t}}export function eachArg(e,r,t){for(const o of e)if(o instanceof g)r.calc.each(o,n=>{t(n,!1)});else if(o instanceof x)for(const n of o.parts)r.calc.each(n,f=>{t(f,!1)});else if(o instanceof i)for(const n of o.data)n!==null&&t(n,!1);else o instanceof p?t(null,!0):o instanceof h?t(a.VALUE,!0):t(o,!0)}export function eachRow(e,r,t){const o=n=>{for(const f of r.calc.matrix(n).data)t(f,!1)};for(const n of e)if(n instanceof g)o(n);else if(n instanceof x)n.parts.forEach(o);else if(n instanceof i)for(const f of n.data)t(f,!1);else n instanceof p?t(null,!0):n instanceof h?t(a.VALUE,!0):t(n,!0)}export function numbersOf(e,r){const t=[];let o;return eachArg(e,r,(n,f)=>{if(!o){if(n instanceof l)o=n;else if(typeof n=="number")t.push(n);else if(f){const s=c(n);s instanceof l?o=s:t.push(s)}}}),o??t}export function numbersOfA(e,r){const t=[];let o;return eachArg(e,r,(n,f)=>{if(!o)if(n instanceof l)o=n;else if(typeof n=="number")t.push(n);else if(typeof n=="boolean")t.push(n?1:0);else if(f){const s=c(n);s instanceof l?o=s:t.push(s)}else typeof n=="string"&&t.push(0)}),o??t}export const number=e=>(...r)=>{const t=[];for(const o of r){const n=c(o);if(n instanceof l)return n;t.push(n)}return e(...t)};export function textArg(e){return A(e)}export function boolArg(e,r){return e==null?r:w(e)}export function numberArg(e,r){return e===void 0?r:c(e)}
2
+ import{CellError as a,ERR as l,Matrix as i}from"../../values.js";import{toBool as w,toNumber as c,toText as A}from"../coerce.js";import{Lambda as h,Missing as p,Reference as g,Union as x}from"../types.js";export const FUNCTIONS=new Map;const d=255;export const many=d;export function signature(e){const r=/^([vr?]*)(?:\(([vr]+)\))?$/u.exec(e),t=r?.[1];if(t===void 0||(t.match(/\?/gu)??[]).length>1)throw new Error(`"${e}" is not a signature.`);const o=m=>Array.from({length:m.length},(y,b)=>m.charAt(b)==="v"?"value":"whole"),n=o(t.replace("?","")),f=o(r?.[2]??""),s=t.indexOf("?"),u=s>=0?s:n.length+f.length;return{fixed:n,group:f,min:u,max:f.length>0?d:n.length}}export function paramAt(e,r){return r<e.fixed.length?e.fixed[r]??"whole":e.group.length===0?"whole":e.group[(r-e.fixed.length)%e.group.length]??"whole"}export function fillsGroup(e,r){return e.group.length===0||r<=e.fixed.length?!0:(r-e.fixed.length)%e.group.length===0}export function scalar(e,r,t,o,n={}){FUNCTIONS.set(e,{name:e,min:r,max:t,kind:"scalar",call:o,from:n.from??"allyhub",...n.errors===!0?{errors:!0}:{},...n.volatile===!0?{volatile:!0}:{}})}export function raw(e,r,t,o={}){const{min:n,max:f,fixed:s,group:u}=signature(r);FUNCTIONS.set(e,{name:e,min:n,max:f,kind:"raw",params:{fixed:s,group:u},call:t,from:o.from??"allyhub",...o.volatile===!0?{volatile:!0}:{}})}export function lazy(e,r,t,o){FUNCTIONS.set(e,{name:e,min:r,max:t,kind:"lazy",call:o,from:"allyhub"})}export function alias(e,r){const t=FUNCTIONS.get(r);t&&FUNCTIONS.set(e,{...t,name:e})}export function cellsOf(e,r){if(e instanceof a)return e;if(e instanceof x)return l.VALUE;if(e instanceof g){const o=Math.min(e.bottom,e.sheet.lastRow);return{rows:Math.max(0,o-e.top+1),cols:e.cols,at:(n,f)=>r.calc.read(e.sheet,e.top+n,e.left+f),reference:e}}if(e instanceof i)return{rows:e.rows,cols:e.cols,at:(o,n)=>e.at(o,n),array:e};if(e instanceof h)return l.VALUE;const t=e instanceof p?null:e;return{rows:1,cols:1,at:()=>t}}export function eachArg(e,r,t){for(const o of e)if(o instanceof g)r.calc.each(o,n=>{t(n,!1)});else if(o instanceof x)for(const n of o.parts)r.calc.each(n,f=>{t(f,!1)});else if(o instanceof i)for(const n of o.data)n!==null&&t(n,!1);else o instanceof p?t(null,!0):o instanceof h?t(l.VALUE,!0):t(o,!0)}export function eachRow(e,r,t){const o=n=>{for(const f of r.calc.matrix(n).data)t(f,!1)};for(const n of e)if(n instanceof g)o(n);else if(n instanceof x)n.parts.forEach(o);else if(n instanceof i)for(const f of n.data)t(f,!1);else n instanceof p?t(null,!0):n instanceof h?t(l.VALUE,!0):t(n,!0)}export function numbersOf(e,r){const t=[];let o;return eachArg(e,r,(n,f)=>{if(!o){if(n instanceof a)o=n;else if(typeof n=="number")t.push(n);else if(f){const s=c(n);s instanceof a?o=s:t.push(s)}}}),o??t}export function numbersOfA(e,r){const t=[];let o;return eachArg(e,r,(n,f)=>{if(!o)if(n instanceof a)o=n;else if(typeof n=="number")t.push(n);else if(typeof n=="boolean")t.push(n?1:0);else if(f){const s=c(n);s instanceof a?o=s:t.push(s)}else typeof n=="string"&&t.push(0)}),o??t}export const number=e=>(...r)=>{const t=[];for(const o of r){const n=c(o);if(n instanceof a)return n;t.push(n)}return e(...t)};export function textArg(e){return A(e)}export function boolArg(e,r){return e==null?r:w(e)}export function numberArg(e,r){return e===void 0?r:c(e)}
@@ -1,2 +1,2 @@
1
1
  /*! AllyHub. Copyright (c) 2026 AllyHub, Inc. All rights reserved. Use is subject to LICENSE.md. */
2
- import{cellName as N}from"../../address.js";import{CellError as s,ERR as n,Matrix as R}from"../../values.js";import{FormulaCell as p}from"../../workbook.js";import{toNumber as L,toText as d}from"../coerce.js";import{Lambda as I,Reference as l,Union as y}from"../types.js";import{lazy as u,raw as m,scalar as c}from"./define.js";const f=(e,r)=>{c(e,1,1,t=>r(t),{errors:!0})};f("ISBLANK",e=>e==null),f("ISNUMBER",e=>typeof e=="number"),f("ISTEXT",e=>typeof e=="string"),f("ISNONTEXT",e=>typeof e!="string"),f("ISLOGICAL",e=>typeof e=="boolean"),f("ISERROR",e=>e instanceof s),f("ISERR",e=>e instanceof s&&e!==n.NA),f("ISNA",e=>e===n.NA);const E=(e,r)=>{const t=L(e);return t instanceof s?t:Math.abs(Math.trunc(t))%2===r};c("ISEVEN",1,1,e=>E(e,0)),c("ISODD",1,1,e=>E(e,1)),c("N",1,1,e=>typeof e=="number"?e:typeof e=="boolean"?e?1:0:e instanceof s?e:0,{errors:!0}),m("NA","",()=>n.NA),m("TYPE","r",(e,r)=>{const t=r.calc.flat(e[0]);return t instanceof R?64:typeof t=="number"||t===null?1:typeof t=="string"?2:typeof t=="boolean"?4:16});const S=[n.NULL,n.DIV0,n.VALUE,n.REF,n.NAME,n.NUM,n.NA];c("ERROR.TYPE",1,1,e=>{const r=e instanceof s?S.indexOf(e):-1;return r<0?n.NA:r+1},{errors:!0}),u("ISREF",1,1,(e,r)=>{const t=e[0];if(!t)return!1;const o=r.calc.eval(t,r.frame);return o instanceof l||o instanceof y});const A=e=>e instanceof l?e.sheet.at(e.top,e.left):void 0;u("ISFORMULA",1,1,(e,r)=>{const t=e[0];if(!t)return n.VALUE;const o=r.calc.eval(t,r.frame);return o instanceof I?n.VALUE:A(o)instanceof p}),u("FORMULATEXT",1,1,(e,r)=>{const t=e[0];if(!t)return n.VALUE;const o=A(r.calc.eval(t,r.frame));return o instanceof p?o.text:n.NA}),m("CELL","v?r",(e,r)=>{const t=d(r.calc.scalar(e[0],r.frame));if(t instanceof s)return t;const o=e[1],a=o instanceof l?o:new l(r.frame.sheet,r.frame.row,r.frame.col,r.frame.row,r.frame.col),i=r.calc.read(a.sheet,a.top,a.left);switch(t.toLowerCase()){case"row":return a.top+1;case"col":return a.left+1;case"address":return N({row:a.top,col:a.left}).replace(/^([A-Z]+)(\d+)$/u,"$$$1$$$2");case"contents":return i;case"type":return i===null?"b":typeof i=="string"?"l":"v";default:return n.VALUE}});
2
+ import{cellName as N}from"../../address.js";import{CellError as s,ERR as n,Matrix as R}from"../../values.js";import{FormulaCell as E}from"../../workbook.js";import{toNumber as L,toText as d}from"../coerce.js";import{Lambda as I,Reference as l,Union as y}from"../types.js";import{lazy as m,raw as i,scalar as c}from"./define.js";const f=(e,r)=>{c(e,1,1,t=>r(t),{errors:!0})};f("ISBLANK",e=>e==null),f("ISNUMBER",e=>typeof e=="number"),f("ISTEXT",e=>typeof e=="string"),f("ISNONTEXT",e=>typeof e!="string"),f("ISLOGICAL",e=>typeof e=="boolean"),f("ISERROR",e=>e instanceof s),f("ISERR",e=>e instanceof s&&e!==n.NA),f("ISNA",e=>e===n.NA);const p=(e,r)=>{const t=L(e);return t instanceof s?t:Math.abs(Math.trunc(t))%2===r};c("ISEVEN",1,1,e=>p(e,0)),c("ISODD",1,1,e=>p(e,1)),c("N",1,1,e=>typeof e=="number"?e:typeof e=="boolean"?e?1:0:e instanceof s?e:0,{errors:!0}),i("NA","",()=>n.NA),i("TYPE","r",(e,r)=>{const t=r.calc.flat(e[0]);return t instanceof R?64:typeof t=="number"||t===null?1:typeof t=="string"?2:typeof t=="boolean"?4:16});const S=[n.NULL,n.DIV0,n.VALUE,n.REF,n.NAME,n.NUM,n.NA];c("ERROR.TYPE",1,1,e=>{const r=e instanceof s?S.indexOf(e):-1;return r<0?n.NA:r+1},{errors:!0}),m("ISREF",1,1,(e,r)=>{const t=e[0];if(!t)return!1;const o=r.calc.eval(t,r.frame);return o instanceof l||o instanceof y});const A=e=>e instanceof l?e.sheet.at(e.top,e.left):void 0;m("ISFORMULA",1,1,(e,r)=>{const t=e[0];if(!t)return n.VALUE;const o=r.calc.eval(t,r.frame);return o instanceof I?n.VALUE:A(o)instanceof E}),m("FORMULATEXT",1,1,(e,r)=>{const t=e[0];if(!t)return n.VALUE;const o=A(r.calc.eval(t,r.frame));return o instanceof E?o.text:n.NA}),i("CELL","v?r",(e,r)=>{const t=d(r.calc.scalar(e[0],r.frame));if(t instanceof s)return t;const o=e[1],a=o instanceof l?o:new l(r.frame.sheet,r.frame.row,r.frame.col,r.frame.row,r.frame.col),u=r.calc.read(a.sheet,a.top,a.left);switch(t.toLowerCase()){case"row":return a.top+1;case"col":return a.left+1;case"address":return N({row:a.top,col:a.left}).replace(/^([A-Z]+)(\d+)$/u,"$$$1$$$2");case"contents":return u;case"type":return u===null?"b":typeof u=="string"?"l":"v";default:return n.VALUE}}),i("TABLE","?rrrrr",()=>n.NA);