@symbo.ls/utils 3.14.777 → 3.14.778

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cjs/state.js CHANGED
@@ -1 +1 @@
1
- "use strict";var a=Object.defineProperty;var m=Object.getOwnPropertyDescriptor;var w=Object.getOwnPropertyNames;var x=Object.prototype.hasOwnProperty;var v=(e,r)=>{for(var o in r)a(e,o,{get:r[o],enumerable:!0})},O=(e,r,o,t)=>{if(r&&typeof r=="object"||typeof r=="function")for(let n of w(r))!x.call(e,n)&&n!==o&&a(e,n,{get:()=>r[n],enumerable:!(t=m(r,n))||t.enumerable});return e};var I=e=>O(a({},"__esModule",{value:!0}),e);var C={};v(C,{applyDependentState:()=>E,checkForStateTypes:()=>j,checkIfInherits:()=>P,createInheritedState:()=>k,createNestedObjectByKeyPath:()=>D,findInheritedState:()=>b,getChildStateInKey:()=>S,getParentStateInKey:()=>h,getRootStateInKey:()=>g,isPolyglotWritableKey:()=>T,isState:()=>A,markPolyglotSeeded:()=>B,overwriteState:()=>K,polyglotSeededKeys:()=>d});module.exports=I(C);var y=require("./array.js"),p=require("./keys.js"),l=require("./object.js"),c=require("./types.js");const j=e=>{const{state:r,props:o,__ref:t}=e,n=o?.state||r;return(0,c.is)(n)("string","number")?(t.__state=n,{value:n}):n===!0?(t.__state=e.key,{}):n?(t.__hasRootState=!0,n):!1},g=(e,r)=>{if(!e.includes("~/"))return;if(e.split("~/").length>1)return r.root},h=(e,r)=>{if(!e.includes("../"))return;const t=e.split("../").length-1;for(let n=0;n<t;n++){if(!r.parent)return null;r=r.parent}return r},S=(e,r,o={})=>{const t=(0,c.isString)(e)?e.split("/"):[e],n=t.length-1;for(let s=0;s<n;s++){const i=t[s],f=t[s+1];if(i==="__proto__"||f==="__proto__")return;let u=r[i];u||(u=r[i]={}),u[f]||(u[f]={}),e=f,r=u}return o.returnParent?r:r[e]},b=(e,r,o={})=>{let n=e.__ref.__state;if(!P(e))return;const s=g(n,r.state);let i=r.state;if(s)i=s,n=n.replaceAll("~/","");else{const f=h(n,r.state);f&&(i=f,n=n.replaceAll("../",""))}if(i)return S(n,i,o)},k=(e,r)=>{const o=e.__ref,t=b(e,r);if(t===void 0)return e.state;if((0,c.is)(t)("object","array"))return(0,l.deepClone)(t);if((0,c.is)(t)("string","number","boolean"))return o.__stateType=typeof t,{value:t};console.warn(o.__state,"is not present. Replacing with",{})},P=e=>{const{__ref:r}=e,o=r?.__state;return!!(o&&(0,c.is)(o)("number","string","boolean"))},A=function(e){return(0,c.isObjectLike)(e)?!!(e.update&&e.parse&&e.clean&&e.create&&e.parent&&e.destroy&&e.rootUpdate&&e.parentUpdate&&e.keys&&e.values&&e.toggle&&e.replace&&e.quietUpdate&&e.quietReplace&&e.add&&e.apply&&e.applyReplace&&e.setByPath&&e.setPathCollection&&e.removeByPath&&e.removePathCollection&&e.getByPath&&e.applyFunction&&e.__element&&e.__children):!1},D=(e,r)=>{if(!e)return r||{};const o=e.split("/"),t={};let n=t;const s=o.length-1;for(let i=0;i<=s;i++)n[o[i]]=i===s?r||{}:{},n=n[o[i]];return t},E=(e,r)=>{const{__element:o}=r,t=o?.state;if(!t)return;const n=(0,l.deepClone)(t,p.STATE_METHODS),s={[e.key]:n},i=(0,c.isObject)(t.__depends)?{...t.__depends,...s}:s;return Array.isArray(t)?(0,y.addProtoToArray)(t,{...Object.getPrototypeOf(t),__depends:i}):Object.setPrototypeOf(t,{...Object.getPrototypeOf(t),__depends:i}),n},K=(e,r,o={})=>{const{overwrite:t}=o;if(!t)return;const n=t==="shallow";if(t==="merge"){(0,l.deepMerge)(e,r,p.STATE_METHODS);return}(n?l.overwriteShallow:l.overwriteDeep)(e,r,p.STATE_METHODS)},_="__polyglotSeededKeys",d=e=>{if(!(0,c.isObject)(e))return new Set;let r=e[_];if(!(r instanceof Set)){r=new Set;try{Object.defineProperty(e,_,{value:r,enumerable:!1,writable:!0,configurable:!0})}catch{}}return r},T=(e,r,o)=>d(e).has(r)?!0:!(0,c.isObject)(o)||o[r]===void 0,B=(e,r)=>(d(e).add(r),r);
1
+ "use strict";var d=Object.defineProperty;var j=Object.getOwnPropertyDescriptor;var v=Object.getOwnPropertyNames;var x=Object.prototype.hasOwnProperty;var E=(e,r)=>{for(var n in r)d(e,n,{get:r[n],enumerable:!0})},A=(e,r,n,t)=>{if(r&&typeof r=="object"||typeof r=="function")for(let o of v(r))!x.call(e,o)&&o!==n&&d(e,o,{get:()=>r[o],enumerable:!(t=j(r,o))||t.enumerable});return e};var I=e=>A(d({},"__esModule",{value:!0}),e);var U={};E(U,{applyDependentState:()=>B,checkForStateTypes:()=>D,checkIfInherits:()=>O,createInheritedState:()=>K,createNestedObjectByKeyPath:()=>T,findInheritedState:()=>m,getChildStateInKey:()=>w,getParentStateInKey:()=>P,getRootStateInKey:()=>b,isPolyglotRetranslatableKey:()=>F,isPolyglotWritableKey:()=>M,isState:()=>R,markPolyglotSeeded:()=>L,overwriteState:()=>C,polyglotSeededKeys:()=>p});module.exports=I(U);var S=require("./array.js"),f=require("./keys.js"),c=require("./object.js"),l=require("./types.js");const D=e=>{const{state:r,props:n,__ref:t}=e,o=n?.state||r;return(0,l.is)(o)("string","number")?(t.__state=o,{value:o}):o===!0?(t.__state=e.key,{}):o?(t.__hasRootState=!0,o):!1},b=(e,r)=>{if(!e.includes("~/"))return;if(e.split("~/").length>1)return r.root},P=(e,r)=>{if(!e.includes("../"))return;const t=e.split("../").length-1;for(let o=0;o<t;o++){if(!r.parent)return null;r=r.parent}return r},w=(e,r,n={})=>{const t=(0,l.isString)(e)?e.split("/"):[e],o=t.length-1;for(let i=0;i<o;i++){const s=t[i],u=t[i+1];if(s==="__proto__"||u==="__proto__")return;let a=r[s];a||(a=r[s]={}),a[u]||(a[u]={}),e=u,r=a}return n.returnParent?r:r[e]},m=(e,r,n={})=>{let o=e.__ref.__state;if(!O(e))return;const i=b(o,r.state);let s=r.state;if(i)s=i,o=o.replaceAll("~/","");else{const u=P(o,r.state);u&&(s=u,o=o.replaceAll("../",""))}if(s)return w(o,s,n)},K=(e,r)=>{const n=e.__ref,t=m(e,r);if(t===void 0)return e.state;if((0,l.is)(t)("object","array"))return(0,c.deepClone)(t);if((0,l.is)(t)("string","number","boolean"))return n.__stateType=typeof t,{value:t};console.warn(n.__state,"is not present. Replacing with",{})},O=e=>{const{__ref:r}=e,n=r?.__state;return!!(n&&(0,l.is)(n)("number","string","boolean"))},R=function(e){return(0,l.isObjectLike)(e)?!!(e.update&&e.parse&&e.clean&&e.create&&e.parent&&e.destroy&&e.rootUpdate&&e.parentUpdate&&e.keys&&e.values&&e.toggle&&e.replace&&e.quietUpdate&&e.quietReplace&&e.add&&e.apply&&e.applyReplace&&e.setByPath&&e.setPathCollection&&e.removeByPath&&e.removePathCollection&&e.getByPath&&e.applyFunction&&e.__element&&e.__children):!1},T=(e,r)=>{if(!e)return r||{};const n=e.split("/"),t={};let o=t;const i=n.length-1;for(let s=0;s<=i;s++)o[n[s]]=s===i?r||{}:{},o=o[n[s]];return t},B=(e,r)=>{const{__element:n}=r,t=n?.state;if(!t)return;const o=(0,c.deepClone)(t,f.STATE_METHODS),i={[e.key]:o},s=(0,l.isObject)(t.__depends)?{...t.__depends,...i}:i;return Array.isArray(t)?(0,S.addProtoToArray)(t,{...Object.getPrototypeOf(t),__depends:s}):Object.setPrototypeOf(t,{...Object.getPrototypeOf(t),__depends:s}),o},C=(e,r,n={})=>{const{overwrite:t}=n;if(!t)return;const o=t==="shallow";if(t==="merge"){(0,c.deepMerge)(e,r,f.STATE_METHODS);return}(o?c.overwriteShallow:c.overwriteDeep)(e,r,f.STATE_METHODS)},_="__polyglotSeededKeys",p=e=>{if(!(0,l.isObject)(e))return new Set;let r=e[_];if(!(r instanceof Set)){r=new Set;try{Object.defineProperty(e,_,{value:r,enumerable:!1,writable:!0,configurable:!0})}catch{}}return r},M=(e,r,n)=>p(e).has(r)?!0:!(0,l.isObject)(n)||n[r]===void 0,g="__polyglotSeededValues",y=e=>{if(!(0,l.isObject)(e))return new Map;let r=e[g];if(!(r instanceof Map)){r=new Map;try{Object.defineProperty(e,g,{value:r,enumerable:!1,writable:!0,configurable:!0})}catch{}}return r},h=e=>{if(e===null||typeof e!="object")return typeof e+":"+String(e);try{return"json:"+JSON.stringify(e)}catch{return null}},F=(e,r,n)=>{if(!p(e).has(r))return!1;const t=y(e);if(!t.has(r))return!0;const o=(0,l.isObject)(n)||Array.isArray(n)?n[r]:void 0,i=h(o);return i!==null&&i===t.get(r)},L=(e,r,...n)=>(p(e).add(r),n.length&&y(e).set(r,h(n[0])),r);
package/dist/esm/state.js CHANGED
@@ -1 +1 @@
1
- import{addProtoToArray as y}from"./array.js";import{STATE_METHODS as u}from"./keys.js";import{deepClone as a,deepMerge as g,overwriteDeep as h,overwriteShallow as S}from"./object.js";import{is as f,isObject as p,isObjectLike as b,isString as P}from"./types.js";const D=e=>{const{state:r,props:o,__ref:t}=e,n=o?.state||r;return f(n)("string","number")?(t.__state=n,{value:n}):n===!0?(t.__state=e.key,{}):n?(t.__hasRootState=!0,n):!1},m=(e,r)=>{if(!e.includes("~/"))return;if(e.split("~/").length>1)return r.root},w=(e,r)=>{if(!e.includes("../"))return;const t=e.split("../").length-1;for(let n=0;n<t;n++){if(!r.parent)return null;r=r.parent}return r},x=(e,r,o={})=>{const t=P(e)?e.split("/"):[e],n=t.length-1;for(let s=0;s<n;s++){const i=t[s],c=t[s+1];if(i==="__proto__"||c==="__proto__")return;let l=r[i];l||(l=r[i]={}),l[c]||(l[c]={}),e=c,r=l}return o.returnParent?r:r[e]},v=(e,r,o={})=>{let n=e.__ref.__state;if(!O(e))return;const s=m(n,r.state);let i=r.state;if(s)i=s,n=n.replaceAll("~/","");else{const c=w(n,r.state);c&&(i=c,n=n.replaceAll("../",""))}if(i)return x(n,i,o)},E=(e,r)=>{const o=e.__ref,t=v(e,r);if(t===void 0)return e.state;if(f(t)("object","array"))return a(t);if(f(t)("string","number","boolean"))return o.__stateType=typeof t,{value:t};console.warn(o.__state,"is not present. Replacing with",{})},O=e=>{const{__ref:r}=e,o=r?.__state;return!!(o&&f(o)("number","string","boolean"))},K=function(e){return b(e)?!!(e.update&&e.parse&&e.clean&&e.create&&e.parent&&e.destroy&&e.rootUpdate&&e.parentUpdate&&e.keys&&e.values&&e.toggle&&e.replace&&e.quietUpdate&&e.quietReplace&&e.add&&e.apply&&e.applyReplace&&e.setByPath&&e.setPathCollection&&e.removeByPath&&e.removePathCollection&&e.getByPath&&e.applyFunction&&e.__element&&e.__children):!1},T=(e,r)=>{if(!e)return r||{};const o=e.split("/"),t={};let n=t;const s=o.length-1;for(let i=0;i<=s;i++)n[o[i]]=i===s?r||{}:{},n=n[o[i]];return t},B=(e,r)=>{const{__element:o}=r,t=o?.state;if(!t)return;const n=a(t,u),s={[e.key]:n},i=p(t.__depends)?{...t.__depends,...s}:s;return Array.isArray(t)?y(t,{...Object.getPrototypeOf(t),__depends:i}):Object.setPrototypeOf(t,{...Object.getPrototypeOf(t),__depends:i}),n},C=(e,r,o={})=>{const{overwrite:t}=o;if(!t)return;const n=t==="shallow";if(t==="merge"){g(e,r,u);return}(n?S:h)(e,r,u)},d="__polyglotSeededKeys",_=e=>{if(!p(e))return new Set;let r=e[d];if(!(r instanceof Set)){r=new Set;try{Object.defineProperty(e,d,{value:r,enumerable:!1,writable:!0,configurable:!0})}catch{}}return r},R=(e,r,o)=>_(e).has(r)?!0:!p(o)||o[r]===void 0,F=(e,r)=>(_(e).add(r),r);export{B as applyDependentState,D as checkForStateTypes,O as checkIfInherits,E as createInheritedState,T as createNestedObjectByKeyPath,v as findInheritedState,x as getChildStateInKey,w as getParentStateInKey,m as getRootStateInKey,R as isPolyglotWritableKey,K as isState,F as markPolyglotSeeded,C as overwriteState,_ as polyglotSeededKeys};
1
+ import{addProtoToArray as S}from"./array.js";import{STATE_METHODS as f}from"./keys.js";import{deepClone as d,deepMerge as b,overwriteDeep as P,overwriteShallow as w}from"./object.js";import{is as a,isObject as u,isObjectLike as m,isString as O}from"./types.js";const T=e=>{const{state:r,props:n,__ref:t}=e,o=n?.state||r;return a(o)("string","number")?(t.__state=o,{value:o}):o===!0?(t.__state=e.key,{}):o?(t.__hasRootState=!0,o):!1},j=(e,r)=>{if(!e.includes("~/"))return;if(e.split("~/").length>1)return r.root},v=(e,r)=>{if(!e.includes("../"))return;const t=e.split("../").length-1;for(let o=0;o<t;o++){if(!r.parent)return null;r=r.parent}return r},x=(e,r,n={})=>{const t=O(e)?e.split("/"):[e],o=t.length-1;for(let i=0;i<o;i++){const s=t[i],l=t[i+1];if(s==="__proto__"||l==="__proto__")return;let c=r[s];c||(c=r[s]={}),c[l]||(c[l]={}),e=l,r=c}return n.returnParent?r:r[e]},E=(e,r,n={})=>{let o=e.__ref.__state;if(!A(e))return;const i=j(o,r.state);let s=r.state;if(i)s=i,o=o.replaceAll("~/","");else{const l=v(o,r.state);l&&(s=l,o=o.replaceAll("../",""))}if(s)return x(o,s,n)},B=(e,r)=>{const n=e.__ref,t=E(e,r);if(t===void 0)return e.state;if(a(t)("object","array"))return d(t);if(a(t)("string","number","boolean"))return n.__stateType=typeof t,{value:t};console.warn(n.__state,"is not present. Replacing with",{})},A=e=>{const{__ref:r}=e,n=r?.__state;return!!(n&&a(n)("number","string","boolean"))},C=function(e){return m(e)?!!(e.update&&e.parse&&e.clean&&e.create&&e.parent&&e.destroy&&e.rootUpdate&&e.parentUpdate&&e.keys&&e.values&&e.toggle&&e.replace&&e.quietUpdate&&e.quietReplace&&e.add&&e.apply&&e.applyReplace&&e.setByPath&&e.setPathCollection&&e.removeByPath&&e.removePathCollection&&e.getByPath&&e.applyFunction&&e.__element&&e.__children):!1},M=(e,r)=>{if(!e)return r||{};const n=e.split("/"),t={};let o=t;const i=n.length-1;for(let s=0;s<=i;s++)o[n[s]]=s===i?r||{}:{},o=o[n[s]];return t},F=(e,r)=>{const{__element:n}=r,t=n?.state;if(!t)return;const o=d(t,f),i={[e.key]:o},s=u(t.__depends)?{...t.__depends,...i}:i;return Array.isArray(t)?S(t,{...Object.getPrototypeOf(t),__depends:s}):Object.setPrototypeOf(t,{...Object.getPrototypeOf(t),__depends:s}),o},L=(e,r,n={})=>{const{overwrite:t}=n;if(!t)return;const o=t==="shallow";if(t==="merge"){b(e,r,f);return}(o?w:P)(e,r,f)},_="__polyglotSeededKeys",p=e=>{if(!u(e))return new Set;let r=e[_];if(!(r instanceof Set)){r=new Set;try{Object.defineProperty(e,_,{value:r,enumerable:!1,writable:!0,configurable:!0})}catch{}}return r},U=(e,r,n)=>p(e).has(r)?!0:!u(n)||n[r]===void 0,g="__polyglotSeededValues",y=e=>{if(!u(e))return new Map;let r=e[g];if(!(r instanceof Map)){r=new Map;try{Object.defineProperty(e,g,{value:r,enumerable:!1,writable:!0,configurable:!0})}catch{}}return r},h=e=>{if(e===null||typeof e!="object")return typeof e+":"+String(e);try{return"json:"+JSON.stringify(e)}catch{return null}},k=(e,r,n)=>{if(!p(e).has(r))return!1;const t=y(e);if(!t.has(r))return!0;const o=u(n)||Array.isArray(n)?n[r]:void 0,i=h(o);return i!==null&&i===t.get(r)},V=(e,r,...n)=>(p(e).add(r),n.length&&y(e).set(r,h(n[0])),r);export{F as applyDependentState,T as checkForStateTypes,A as checkIfInherits,B as createInheritedState,M as createNestedObjectByKeyPath,E as findInheritedState,x as getChildStateInKey,v as getParentStateInKey,j as getRootStateInKey,k as isPolyglotRetranslatableKey,U as isPolyglotWritableKey,C as isState,V as markPolyglotSeeded,L as overwriteState,p as polyglotSeededKeys};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@symbo.ls/utils",
3
- "version": "3.14.777",
3
+ "version": "3.14.778",
4
4
  "license": "CC-BY-NC-4.0",
5
5
  "type": "module",
6
6
  "module": "./dist/esm/index.js",
package/state.js CHANGED
@@ -250,10 +250,14 @@ export const polyglotSeededKeys = (context) => {
250
250
  }
251
251
 
252
252
  /**
253
- * May polyglot write `key` into the root state? Only when polyglot seeded it
254
- * before, or when nothing declares it yet. A key a project, an app or a
253
+ * May BOOT seeding write `key` into the root state? Only when polyglot seeded
254
+ * it before, or when nothing declares it yet. A key a project, an app or a
255
255
  * shared library declares is never polyglot's to overwrite.
256
256
  *
257
+ * A language SWITCH asks `isPolyglotRetranslatableKey` instead: "nothing
258
+ * declares it yet" is true of every app flag that is not in state.js, so on a
259
+ * switch it handed any such flag whose NAME matched a text key that key's text.
260
+ *
257
261
  * @param {object} context - the app context carrying the seeded-key record
258
262
  * @param {string} key - the translation key about to be written
259
263
  * @param {object} [declared] - the state to test the key against
@@ -263,11 +267,63 @@ export const isPolyglotWritableKey = (context, key, declared) => {
263
267
  return !isObject(declared) || declared[key] === undefined
264
268
  }
265
269
 
270
+ // What polyglot last wrote into each key it seeded — a signature, so a store
271
+ // proxy over the same array or object still matches. Non-enumerable on the
272
+ // context, like SEEDED_KEYS.
273
+ const SEEDED_VALUES = '__polyglotSeededValues'
274
+ const seededValues = (context) => {
275
+ if (!isObject(context)) return new Map()
276
+ let values = context[SEEDED_VALUES]
277
+ if (!(values instanceof Map)) {
278
+ values = new Map()
279
+ try {
280
+ Object.defineProperty(context, SEEDED_VALUES, {
281
+ value: values,
282
+ enumerable: false,
283
+ writable: true,
284
+ configurable: true
285
+ })
286
+ } catch (e) { /* a frozen context keeps no record — see polyglotSeededKeys */ }
287
+ }
288
+ return values
289
+ }
290
+ const seededSignature = (value) => {
291
+ if (value === null || typeof value !== 'object') return typeof value + ':' + String(value)
292
+ try { return 'json:' + JSON.stringify(value) } catch (e) { return null }
293
+ }
294
+
295
+ /**
296
+ * May a language SWITCH (setLang) write `key` into the root state? Only when
297
+ * polyglot seeded the key itself AND the slot still holds exactly what
298
+ * polyglot wrote. Never by name alone:
299
+ * - a key polyglot never seeded is the app's, even when it is empty — an
300
+ * undeclared app flag whose name matches a text key stays untouched
301
+ * (measured on dev, CRM app 1.0.87, 2026-10-01: an en <-> ka switch wrote
302
+ * "Delete deal" into the `crmDealDelete` open flag and opened the dialog);
303
+ * - a seeded key the app has since set to its own value is the app's now.
304
+ * A bare `{{ key }}` template still translates: the template resolver falls
305
+ * back to the active translation map when the state holds nothing.
306
+ *
307
+ * @param {object} context - the app context carrying the seeded-key record
308
+ * @param {string} key - the translation key about to be written
309
+ * @param {object} [current] - the root state holding the key now
310
+ */
311
+ export const isPolyglotRetranslatableKey = (context, key, current) => {
312
+ if (!polyglotSeededKeys(context).has(key)) return false
313
+ const values = seededValues(context)
314
+ if (!values.has(key)) return true // seeded by a caller that recorded no value
315
+ const now = isObject(current) || Array.isArray(current) ? current[key] : undefined
316
+ const sig = seededSignature(now)
317
+ return sig !== null && sig === values.get(key)
318
+ }
319
+
266
320
  /**
267
- * Record that polyglot seeded `key`, so a later language switch may retranslate
268
- * it. Returns the key, for use inline.
321
+ * Record that polyglot seeded `key` (and, when given, the value it wrote), so
322
+ * a later language switch may retranslate it while the slot still holds that
323
+ * value. Returns the key, for use inline.
269
324
  */
270
- export const markPolyglotSeeded = (context, key) => {
325
+ export const markPolyglotSeeded = (context, key, ...written) => {
271
326
  polyglotSeededKeys(context).add(key)
327
+ if (written.length) seededValues(context).set(key, seededSignature(written[0]))
272
328
  return key
273
329
  }