@sveltekit-i18n/parser-mf2 3.0.0 → 3.0.1

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/README.md CHANGED
@@ -359,7 +359,7 @@ A report carries `code`, the `key` and `locale` the call was made for, a one-sen
359
359
 
360
360
  | `code` | When |
361
361
  | --- | --- |
362
- | `failed-message` | The message could not be compiled - a syntax error, or a data model error such as a selector without an annotation or a `.match` without a `*` variant - or could not be formatted at all. The raw message is returned. |
362
+ | `failed-message` | The message could not be compiled - a syntax error, or a data model error such as a selector without an annotation or a `.match` without a `*` variant - or could not be formatted at all. The raw message is returned; a leaf that is not text and cannot become any renders as the empty string. |
363
363
  | `fallback-value` | One expression could not be resolved: a variable the payload lacks, a function nobody registered, or a function that rejected its operand or options. The rest of the message rendered, and the output carries the format's fallback for that expression, `{$name}`. |
364
364
 
365
365
  ```javascript
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- import{MessageFormat as b}from"messageformat";import{DraftFunctions as x}from"messageformat/functions";import{parseMessage as k,validate as w}from"messageformat";var v=i=>{switch(i){case"number":case"integer":case"offset":case"currency":case"percent":case"unit":return"number";case"string":return"string";case"date":case"datetime":case"time":return"date";default:return"unknown"}},B=/^-?(?:0|[1-9]\d*)(?:\.\d+)?$/,E=(i,a)=>{let m=Array.isArray(i)?i:[i],u=a==="unknown"?m:[...m.filter(p=>p!=="unknown"),a],s=[...new Set(u)];return s.length===1?s[0]:s},K=()=>i=>{if(typeof i!="string")return[];let a;try{a=k(i),w(a)}catch{return[]}let m=new Map,u=e=>m.get(e)??{param:e};a.declarations.forEach(({type:e,name:t,value:{arg:r,functionRef:n}})=>{let d=e==="input"?{param:t}:r?.type==="variable"?u(r.name):{};m.set(t,{param:d.param,fn:n?.name??d.fn})});let s=[],p=new Map,y=(e,t,r,n)=>{let d=p.get(e);if(d===void 0){s.push(e),p.set(e,{name:e,kind:t,...n?{values:n}:{},optional:r.length>0,...r.length>0?{when:r}:{}});return}let{when:P,...h}=d,g=r.length>0&&P!==void 0;p.set(e,{...h,kind:E(d.kind??"unknown",t),...n?{values:[...new Set([...d.values??[],...n])]}:{},optional:g,...g?{when:P}:{}})},o=(e,t)=>{Object.values(e??{}).forEach(r=>{if(r.type!=="variable")return;let{param:n}=u(r.name);n!==void 0&&y(n,"unknown",t)})},l=({arg:e,functionRef:t},r)=>{if(e?.type==="variable"){let{param:n}=u(e.name);n!==void 0&&y(n,v(t?.name),r)}o(t?.options,r)},f=(e,t)=>{e.forEach(r=>{if(typeof r!="string"){if(r.type==="markup"){o(r.options,t);return}l(r,t)}})};if(a.declarations.forEach(({value:e})=>l(e,[])),a.type==="message")return f(a.pattern,[]),s.map(e=>p.get(e));let c=a.selectors.map(({name:e},t)=>{let{param:r,fn:n}=u(e),d=v(n),P=a.variants.flatMap(({keys:g})=>{let M=g[t];return M?.type==="literal"?[M.value]:[]}),h=d==="string"?P:d==="number"?P.filter(g=>B.test(g)):[];return r!==void 0&&y(r,d,[],h.length>0?h:void 0),r??e});return a.variants.forEach(({keys:e,value:t})=>{f(t,e.map((r,n)=>({param:c[n],branch:r.type==="*"?"*":r.value})))}),s.map(e=>p.get(e))};var F=1e4,S=({onReport:i,functions:a,...m})=>{let u={...m,functions:{...x,...a}},s=new Map,p=o=>{if(i)try{i(o)}catch{return}},y=(o,l)=>{if(typeof o!="string")return new b(l,o,u);let f=`${l}\0${o}`,c=s.get(f);if(c===void 0){if(c=new b(l,o,u),s.size>=F){let e=s.keys().next().value;e!==void 0&&s.delete(e)}}else s.delete(f);return s.set(f,c),c};return{parse:(o,[l],f,c)=>{if(o===void 0)return"";try{return y(o,f).format(l,e=>{p({code:"fallback-value",key:c,locale:f,message:`An expression in the message for key '${c}' could not be resolved and was rendered as its fallback.`,error:e})})}catch(e){return p({code:"failed-message",key:c,locale:f,message:`Message for key '${c}' could not be formatted and was returned raw.`,error:e}),String(o)}}}},I=S;export{I as default,K as extractParamsFactory};
1
+ import{MessageFormat as b}from"messageformat";import{DraftFunctions as K}from"messageformat/functions";import{parseMessage as k,validate as v}from"messageformat";var w=o=>{switch(o){case"number":case"integer":case"offset":case"currency":case"percent":case"unit":return"number";case"string":return"string";case"date":case"datetime":case"time":return"date";default:return"unknown"}},B=/^-?(?:0|[1-9]\d*)(?:\.\d+)?$/,x=(o,a)=>{let m=Array.isArray(o)?o:[o],p=a==="unknown"?m:[...m.filter(u=>u!=="unknown"),a],s=[...new Set(p)];return s.length===1?s[0]:s},E=()=>o=>{if(typeof o!="string")return[];let a;try{a=k(o),v(a)}catch{return[]}let m=new Map,p=e=>m.get(e)??{param:e};a.declarations.forEach(({type:e,name:n,value:{arg:r,functionRef:t}})=>{let d=e==="input"?{param:n}:r?.type==="variable"?p(r.name):{};m.set(n,{param:d.param,fn:t?.name??d.fn})});let s=[],u=new Map,y=(e,n,r,t)=>{let d=u.get(e);if(d===void 0){s.push(e),u.set(e,{name:e,kind:n,...t?{values:t}:{},optional:r.length>0,...r.length>0?{when:r}:{}});return}let{when:h,...P}=d,g=r.length>0&&h!==void 0;u.set(e,{...P,kind:x(d.kind??"unknown",n),...t?{values:[...new Set([...d.values??[],...t])]}:{},optional:g,...g?{when:h}:{}})},i=(e,n)=>{Object.values(e??{}).forEach(r=>{if(r.type!=="variable")return;let{param:t}=p(r.name);t!==void 0&&y(t,"unknown",n)})},l=({arg:e,functionRef:n},r)=>{if(e?.type==="variable"){let{param:t}=p(e.name);t!==void 0&&y(t,w(n?.name),r)}i(n?.options,r)},f=(e,n)=>{e.forEach(r=>{if(typeof r!="string"){if(r.type==="markup"){i(r.options,n);return}l(r,n)}})};if(a.declarations.forEach(({value:e})=>l(e,[])),a.type==="message")return f(a.pattern,[]),s.map(e=>u.get(e));let c=a.selectors.map(({name:e},n)=>{let{param:r,fn:t}=p(e),d=w(t),h=a.variants.flatMap(({keys:g})=>{let M=g[n];return M?.type==="literal"?[M.value]:[]}),P=d==="string"?h:d==="number"?h.filter(g=>B.test(g)):[];return r!==void 0&&y(r,d,[],P.length>0?P:void 0),r??e});return a.variants.forEach(({keys:e,value:n})=>{f(n,e.map((r,t)=>({param:c[t],branch:r.type==="*"?"*":r.value})))}),s.map(e=>u.get(e))};var F=1e4,S=o=>{try{return String(o)}catch{return}},$=({onReport:o,functions:a,...m})=>{let p={...m,functions:{...K,...a}},s=new Map,u=i=>{if(o)try{o(i)}catch{return}},y=(i,l)=>{if(typeof i!="string")return new b(l,i,p);let f=`${l}\0${i}`,c=s.get(f);if(c===void 0){if(c=new b(l,i,p),s.size>=F){let e=s.keys().next().value;e!==void 0&&s.delete(e)}}else s.delete(f);return s.set(f,c),c};return{parse:(i,[l],f,c)=>{if(i===void 0)return"";try{return y(i,f).format(l,e=>{u({code:"fallback-value",key:c,locale:f,message:`An expression in the message for key '${c}' could not be resolved and was rendered as its fallback.`,error:e})})}catch(e){let n=S(i);return u({code:"failed-message",key:c,locale:f,message:n===void 0?`Message for key '${c}' could not be rendered as text.`:`Message for key '${c}' could not be formatted and was returned raw.`,error:e}),n??""}}}},L=$;export{L as default,E as extractParamsFactory};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sveltekit-i18n/parser-mf2",
3
- "version": "3.0.0",
3
+ "version": "3.0.1",
4
4
  "description": "Unicode MessageFormat 2 parser compatible with sveltekit-i18n library.",
5
5
  "type": "module",
6
6
  "sideEffects": false,