snice 2.2.2 → 2.2.3

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 (41) hide show
  1. package/dist/index.cjs +163 -129
  2. package/dist/index.cjs.map +1 -1
  3. package/dist/index.esm.js +163 -129
  4. package/dist/index.esm.js.map +1 -1
  5. package/dist/index.iife.js +163 -129
  6. package/dist/index.iife.js.map +1 -1
  7. package/dist/symbols.cjs.map +1 -1
  8. package/dist/symbols.esm.js +1 -1
  9. package/dist/symbols.esm.js.map +1 -1
  10. package/dist/transitions.cjs.map +1 -1
  11. package/dist/transitions.esm.js +1 -1
  12. package/dist/transitions.esm.js.map +1 -1
  13. package/dist/types/controller.d.ts +1 -7
  14. package/dist/types/element.d.ts +3 -44
  15. package/dist/types/events.d.ts +2 -26
  16. package/dist/types/global.d.ts +1 -5
  17. package/dist/types/index.d.ts +2 -8
  18. package/dist/types/observe.d.ts +1 -16
  19. package/dist/types/request-response.d.ts +2 -28
  20. package/dist/types/router.d.ts +2 -81
  21. package/dist/types/transitions.d.ts +2 -30
  22. package/dist/types/types/DispatchOptions.d.ts +10 -0
  23. package/dist/types/types/IController.d.ts +8 -0
  24. package/dist/types/types/ObserveOptions.d.ts +16 -0
  25. package/dist/types/types/OnOptions.d.ts +16 -0
  26. package/dist/types/types/PageOptions.d.ts +30 -0
  27. package/dist/types/types/PartOptions.d.ts +4 -0
  28. package/dist/types/types/PropertyConverter.d.ts +4 -0
  29. package/dist/types/types/PropertyOptions.d.ts +9 -0
  30. package/dist/types/types/QueryOptions.d.ts +4 -0
  31. package/dist/types/types/RequestOptions.d.ts +18 -0
  32. package/dist/types/types/RespondOptions.d.ts +10 -0
  33. package/dist/types/types/RouterInstance.d.ts +10 -0
  34. package/dist/types/types/RouterOptions.d.ts +32 -0
  35. package/dist/types/types/SimpleArray.d.ts +17 -0
  36. package/dist/types/types/SniceElement.d.ts +8 -0
  37. package/dist/types/types/SniceGlobal.d.ts +5 -0
  38. package/dist/types/types/Transition.d.ts +33 -0
  39. package/dist/types/types/index.d.ts +17 -0
  40. package/dist/types/utils.d.ts +16 -0
  41. package/package.json +2 -3
package/dist/index.cjs CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * snice v2.2.1
2
+ * snice v2.2.2
3
3
  * Imperative TypeScript framework for building vanilla web components with decorators, routing, and controllers. No virtual DOM, no build complexity.
4
4
  * (c) 2024
5
5
  * Released under the MIT License.
@@ -703,6 +703,7 @@ function cleanupObservers(instance) {
703
703
  }
704
704
  }
705
705
 
706
+ // @request decorator transforms methods to return Promise<T>
706
707
  /**
707
708
  * Decorator for making requests from elements or controllers.
708
709
  * Uses async generator pattern for bidirectional communication.
@@ -1239,6 +1240,133 @@ function cleanupNativeElementControllers() {
1239
1240
  }
1240
1241
  }
1241
1242
 
1243
+ /**
1244
+ * SimpleArray type for arrays that can be safely reflected to attributes
1245
+ * Supports arrays of: string, number, boolean
1246
+ * Uses full-width comma (,) as separator to avoid conflicts
1247
+ * Strings cannot contain the full-width comma character
1248
+ */
1249
+ class SimpleArray {
1250
+ static { this.SEPARATOR = ','; } // U+FF0C Full-width comma
1251
+ /**
1252
+ * Serialize array to string for attribute storage
1253
+ */
1254
+ static serialize(arr) {
1255
+ if (!Array.isArray(arr))
1256
+ return '';
1257
+ return arr.map(item => {
1258
+ if (typeof item === 'string') {
1259
+ // Validate string doesn't contain our separator
1260
+ if (item.includes(SimpleArray.SEPARATOR)) {
1261
+ throw new Error(`SimpleArray strings cannot contain the character "${SimpleArray.SEPARATOR}" (U+FF0C)`);
1262
+ }
1263
+ return item;
1264
+ }
1265
+ else if (typeof item === 'number' || typeof item === 'boolean') {
1266
+ return String(item);
1267
+ }
1268
+ else {
1269
+ throw new Error(`SimpleArray only supports string, number, and boolean types. Got: ${typeof item}`);
1270
+ }
1271
+ }).join(SimpleArray.SEPARATOR);
1272
+ }
1273
+ /**
1274
+ * Parse string from attribute back to array
1275
+ */
1276
+ static parse(str) {
1277
+ if (str === null || str === undefined)
1278
+ return [];
1279
+ // Empty string should not be parsed as containing an empty string
1280
+ // since empty arrays don't get reflected (handled by the reflection logic)
1281
+ if (str === '')
1282
+ return [];
1283
+ return str.split(SimpleArray.SEPARATOR).map(item => {
1284
+ // Try to parse as number
1285
+ if (/^-?\d+\.?\d*$/.test(item)) {
1286
+ const num = Number(item);
1287
+ if (!isNaN(num))
1288
+ return num;
1289
+ }
1290
+ // Parse as boolean
1291
+ if (item === 'true')
1292
+ return true;
1293
+ if (item === 'false')
1294
+ return false;
1295
+ // Default to string
1296
+ return item;
1297
+ });
1298
+ }
1299
+ }
1300
+
1301
+ /**
1302
+ * Detects the type constructor from an initial value
1303
+ * @param initialValue - The default value assigned to a property field (e.g., `name = "default"` -> "default")
1304
+ * @returns The constructor function (String, Number, Boolean, etc.) or undefined if type can't be determined
1305
+ */
1306
+ function detectType(initialValue) {
1307
+ if (initialValue === null || initialValue === undefined) {
1308
+ return undefined;
1309
+ }
1310
+ if (typeof initialValue === 'string') {
1311
+ return String;
1312
+ }
1313
+ if (typeof initialValue === 'number') {
1314
+ return Number;
1315
+ }
1316
+ if (typeof initialValue === 'boolean') {
1317
+ return Boolean;
1318
+ }
1319
+ if (initialValue instanceof Date) {
1320
+ return Date;
1321
+ }
1322
+ if (typeof initialValue === 'bigint') {
1323
+ return BigInt;
1324
+ }
1325
+ if (Array.isArray(initialValue)) {
1326
+ return Array;
1327
+ }
1328
+ return undefined;
1329
+ }
1330
+ /**
1331
+ * Parses an attribute value based on the property type
1332
+ * @param attributeValue - The raw string value from the HTML attribute (e.g., "123", "true", "hello")
1333
+ * @param propertyOptions - The options from @property decorator (contains explicit type, attribute name, etc.)
1334
+ * @param currentValue - The current stored value of the property (used for type inference when no explicit type)
1335
+ * @param initialValue - The default value assigned to the property field (used for type detection as fallback)
1336
+ * @returns The parsed value in the correct JavaScript type
1337
+ */
1338
+ function parseAttributeValue(attributeValue, propertyOptions, currentValue, initialValue) {
1339
+ // Use explicit type or detect from initial value
1340
+ const typeToUse = propertyOptions.type || detectType(initialValue);
1341
+ switch (typeToUse) {
1342
+ case Boolean:
1343
+ return attributeValue !== null && attributeValue !== 'false';
1344
+ case Number:
1345
+ return attributeValue !== null ? Number(attributeValue) : null;
1346
+ case String:
1347
+ return attributeValue;
1348
+ case Date:
1349
+ return attributeValue ? new Date(attributeValue) : null;
1350
+ case BigInt:
1351
+ if (attributeValue && attributeValue.endsWith('n')) {
1352
+ return BigInt(attributeValue.slice(0, -1));
1353
+ }
1354
+ else {
1355
+ return attributeValue ? BigInt(attributeValue) : null;
1356
+ }
1357
+ case SimpleArray:
1358
+ return SimpleArray.parse(attributeValue);
1359
+ default:
1360
+ // If no type specified and can't detect, try to infer from current value type
1361
+ if (typeof currentValue === 'number' && attributeValue !== null) {
1362
+ return Number(attributeValue);
1363
+ }
1364
+ else {
1365
+ return attributeValue;
1366
+ }
1367
+ }
1368
+ }
1369
+
1242
1370
  /**
1243
1371
  * Applies core element functionality to a constructor
1244
1372
  * This is shared between @element and @page decorators
@@ -1320,42 +1448,17 @@ function applyElementFunctionality(constructor) {
1320
1448
  const properties = constructor[PROPERTIES];
1321
1449
  if (properties) {
1322
1450
  for (const [propName, propOptions] of properties) {
1323
- // If attribute exists, it always wins
1324
- if (this.hasAttribute(propName)) {
1451
+ // Check for attribute using proper attribute name
1452
+ const attributeName = typeof propOptions.attribute === 'string' ? propOptions.attribute : propName.toLowerCase();
1453
+ if (this.hasAttribute(attributeName)) {
1325
1454
  // Attribute exists, parse and set the property value
1326
- const attrValue = this.getAttribute(propName);
1455
+ const attrValue = this.getAttribute(attributeName);
1327
1456
  // Mark as explicitly set since it came from an attribute
1328
1457
  if (!this[EXPLICITLY_SET_PROPERTIES]) {
1329
1458
  this[EXPLICITLY_SET_PROPERTIES] = new Set();
1330
1459
  }
1331
1460
  this[EXPLICITLY_SET_PROPERTIES].add(propName);
1332
- switch (propOptions.type) {
1333
- case Boolean:
1334
- this[propName] = attrValue !== null && attrValue !== 'false';
1335
- break;
1336
- case Number:
1337
- this[propName] = Number(attrValue);
1338
- break;
1339
- case String:
1340
- this[propName] = attrValue;
1341
- break;
1342
- case Date:
1343
- this[propName] = attrValue ? new Date(attrValue) : null;
1344
- break;
1345
- case BigInt:
1346
- if (attrValue && attrValue.endsWith('n')) {
1347
- this[propName] = BigInt(attrValue.slice(0, -1));
1348
- }
1349
- else {
1350
- this[propName] = attrValue ? BigInt(attrValue) : null;
1351
- }
1352
- break;
1353
- case SimpleArray:
1354
- this[propName] = SimpleArray.parse(attrValue);
1355
- break;
1356
- default:
1357
- this[propName] = attrValue;
1358
- }
1461
+ this[propName] = parseAttributeValue(attrValue, propOptions);
1359
1462
  }
1360
1463
  }
1361
1464
  }
@@ -1530,40 +1633,11 @@ function applyElementFunctionality(constructor) {
1530
1633
  if (properties) {
1531
1634
  for (const [propName, propOptions] of properties) {
1532
1635
  const attributeName = typeof propOptions.attribute === 'string' ? propOptions.attribute : propName.toLowerCase();
1533
- if (attributeName === name) {
1636
+ if (attributeName.toLowerCase() === name.toLowerCase()) {
1534
1637
  // Check if the current property value already matches to avoid feedback loops
1535
1638
  const currentValue = this[PROPERTY_VALUES]?.[propName];
1536
1639
  // Parse the new value based on type
1537
- let parsedValue;
1538
- if (propOptions.type === Boolean) {
1539
- parsedValue = newValue !== null && newValue !== 'false';
1540
- }
1541
- else if (propOptions.type === Number) {
1542
- parsedValue = Number(newValue);
1543
- }
1544
- else if (propOptions.type === Date) {
1545
- parsedValue = newValue ? new Date(newValue) : null;
1546
- }
1547
- else if (propOptions.type === BigInt) {
1548
- if (newValue && newValue.endsWith('n')) {
1549
- parsedValue = BigInt(newValue.slice(0, -1));
1550
- }
1551
- else {
1552
- parsedValue = newValue ? BigInt(newValue) : null;
1553
- }
1554
- }
1555
- else if (propOptions.type === SimpleArray) {
1556
- parsedValue = SimpleArray.parse(newValue);
1557
- }
1558
- else {
1559
- // If no type specified, try to infer from current value type
1560
- if (typeof currentValue === 'number' && newValue !== null) {
1561
- parsedValue = Number(newValue);
1562
- }
1563
- else {
1564
- parsedValue = newValue;
1565
- }
1566
- }
1640
+ const parsedValue = parseAttributeValue(newValue, propOptions, currentValue, undefined);
1567
1641
  // Only update if the value actually changed and avoid infinite loops
1568
1642
  if (currentValue !== parsedValue) {
1569
1643
  // Mark as explicitly set since it came from an attribute change
@@ -1662,11 +1736,18 @@ function property(options) {
1662
1736
  if (options?.reflect && options?.type === Object) {
1663
1737
  console.warn(`⚠️ Property '${propertyKey}' uses reflect:true with Object type.`);
1664
1738
  }
1665
- context.addInitializer(function () {
1666
- // No longer need warnings here since they're at decoration time
1667
- });
1668
1739
  // Return a field initializer function for new decorators
1669
1740
  return function (initialValue) {
1741
+ // Detect type from initial value if not explicitly provided
1742
+ const finalOptions = { ...options };
1743
+ if (!finalOptions.type && initialValue !== undefined) {
1744
+ finalOptions.type = detectType(initialValue);
1745
+ // Update the metadata with the detected type
1746
+ const constructor = this.constructor;
1747
+ if (constructor[PROPERTIES]) {
1748
+ constructor[PROPERTIES].set(propertyKey, finalOptions);
1749
+ }
1750
+ }
1670
1751
  // Set up the property descriptor on first access
1671
1752
  if (!Object.hasOwnProperty.call(this.constructor.prototype, propertyKey)) {
1672
1753
  const descriptor = {
@@ -1674,7 +1755,18 @@ function property(options) {
1674
1755
  if (!this[PROPERTY_VALUES]) {
1675
1756
  this[PROPERTY_VALUES] = {};
1676
1757
  }
1677
- return this[PROPERTY_VALUES][propertyKey];
1758
+ // If we have a stored value, return it
1759
+ if (this[PROPERTY_VALUES][propertyKey] !== undefined) {
1760
+ return this[PROPERTY_VALUES][propertyKey];
1761
+ }
1762
+ // Otherwise check attribute and parse it, or return initial value
1763
+ const attributeName = typeof finalOptions?.attribute === 'string' ? finalOptions?.attribute : propertyKey.toLowerCase();
1764
+ const attrValue = this.getAttribute?.(attributeName);
1765
+ // If attribute exists or we have a type that needs special handling for null (like Boolean)
1766
+ if (attrValue !== null || finalOptions?.type === Boolean) {
1767
+ return parseAttributeValue(attrValue, finalOptions || {}, undefined, initialValue);
1768
+ }
1769
+ return initialValue;
1678
1770
  },
1679
1771
  set(newValue) {
1680
1772
  if (!this[PROPERTY_VALUES]) {
@@ -1691,10 +1783,10 @@ function property(options) {
1691
1783
  this[EXPLICITLY_SET_PROPERTIES].add(propertyKey);
1692
1784
  }
1693
1785
  this[PROPERTY_VALUES][propertyKey] = newValue;
1694
- if (options?.reflect && this.setAttribute && this[PROPERTIES_INITIALIZED] && this[EXPLICITLY_SET_PROPERTIES].has(propertyKey)) {
1695
- const attributeName = typeof options.attribute === 'string' ? options.attribute : propertyKey.toLowerCase();
1786
+ if (finalOptions?.reflect && this.setAttribute && this[PROPERTIES_INITIALIZED] && this[EXPLICITLY_SET_PROPERTIES].has(propertyKey)) {
1787
+ const attributeName = typeof finalOptions.attribute === 'string' ? finalOptions.attribute : propertyKey.toLowerCase();
1696
1788
  if (newValue === null || newValue === undefined || newValue === false ||
1697
- (options?.type === SimpleArray && Array.isArray(newValue) && newValue.length === 0)) {
1789
+ (finalOptions?.type === SimpleArray && Array.isArray(newValue) && newValue.length === 0)) {
1698
1790
  this.removeAttribute(attributeName);
1699
1791
  }
1700
1792
  else {
@@ -1705,7 +1797,7 @@ function property(options) {
1705
1797
  else if (typeof newValue === 'bigint') {
1706
1798
  attributeValue = newValue.toString() + 'n';
1707
1799
  }
1708
- else if (options?.type === SimpleArray && Array.isArray(newValue)) {
1800
+ else if (finalOptions?.type === SimpleArray && Array.isArray(newValue)) {
1709
1801
  attributeValue = SimpleArray.serialize(newValue);
1710
1802
  }
1711
1803
  else {
@@ -1833,63 +1925,6 @@ function queryAll(selector, options = {}) {
1833
1925
  };
1834
1926
  };
1835
1927
  }
1836
- /**
1837
- * SimpleArray type for arrays that can be safely reflected to attributes
1838
- * Supports arrays of: string, number, boolean
1839
- * Uses full-width comma (,) as separator to avoid conflicts
1840
- * Strings cannot contain the full-width comma character
1841
- */
1842
- class SimpleArray {
1843
- static { this.SEPARATOR = ','; } // U+FF0C Full-width comma
1844
- /**
1845
- * Serialize array to string for attribute storage
1846
- */
1847
- static serialize(arr) {
1848
- if (!Array.isArray(arr))
1849
- return '';
1850
- return arr.map(item => {
1851
- if (typeof item === 'string') {
1852
- // Validate string doesn't contain our separator
1853
- if (item.includes(SimpleArray.SEPARATOR)) {
1854
- throw new Error(`SimpleArray strings cannot contain the character "${SimpleArray.SEPARATOR}" (U+FF0C)`);
1855
- }
1856
- return item;
1857
- }
1858
- else if (typeof item === 'number' || typeof item === 'boolean') {
1859
- return String(item);
1860
- }
1861
- else {
1862
- throw new Error(`SimpleArray only supports string, number, and boolean types. Got: ${typeof item}`);
1863
- }
1864
- }).join(SimpleArray.SEPARATOR);
1865
- }
1866
- /**
1867
- * Parse string from attribute back to array
1868
- */
1869
- static parse(str) {
1870
- if (str === null || str === undefined)
1871
- return [];
1872
- // Empty string should not be parsed as containing an empty string
1873
- // since empty arrays don't get reflected (handled by the reflection logic)
1874
- if (str === '')
1875
- return [];
1876
- return str.split(SimpleArray.SEPARATOR).map(item => {
1877
- // Try to parse as number
1878
- if (/^-?\d+\.?\d*$/.test(item)) {
1879
- const num = Number(item);
1880
- if (!isNaN(num))
1881
- return num;
1882
- }
1883
- // Parse as boolean
1884
- if (item === 'true')
1885
- return true;
1886
- if (item === 'false')
1887
- return false;
1888
- // Default to string
1889
- return item;
1890
- });
1891
- }
1892
- }
1893
1928
  function watch(...propertyNames) {
1894
1929
  return function (target, context) {
1895
1930
  const methodName = context.name;
@@ -3192,8 +3227,7 @@ function Router(options) {
3192
3227
  page,
3193
3228
  initialize,
3194
3229
  navigate,
3195
- register,
3196
- context,
3230
+ register
3197
3231
  };
3198
3232
  }
3199
3233