tiny-essentials 1.22.4 → 1.22.5

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.
@@ -5843,6 +5843,277 @@ class TinyHtml {
5843
5843
  const propName = typeof TinyHtml.attrFix[name] === 'string' ? TinyHtml.attrFix[name] : name;
5844
5844
  return propName;
5845
5845
  }
5846
+ ////////////////////////////////////////////////////////////////////
5847
+ /**
5848
+ * Returns the BigInt value of an attribute.
5849
+ * @param {TinyElement} el
5850
+ * @param {string} name
5851
+ * @returns {bigint|null}
5852
+ */
5853
+ static attrBigInt(el, name) {
5854
+ const elem = TinyHtml._preElem(el, 'attrBigInt');
5855
+ const val = elem.getAttribute(TinyHtml.getAttrName(name));
5856
+ if (val == null)
5857
+ return null;
5858
+ try {
5859
+ return BigInt(val);
5860
+ }
5861
+ catch {
5862
+ return null;
5863
+ }
5864
+ }
5865
+ /**
5866
+ * Returns the BigInt value of an attribute.
5867
+ * @param {string} name
5868
+ * @returns {bigint|null}
5869
+ */
5870
+ attrBigInt(name) {
5871
+ return TinyHtml.attrBigInt(this, name);
5872
+ }
5873
+ /**
5874
+ * Set a BigInt attribute.
5875
+ * @template {TinyElement|TinyElement[]} T
5876
+ * @param {T} el
5877
+ * @param {string} name
5878
+ * @param {bigint} value
5879
+ * @returns {T}
5880
+ */
5881
+ static setAttrBigInt(el, name, value) {
5882
+ if (typeof value !== 'bigint')
5883
+ throw new Error('Value is not a valid BigInt.');
5884
+ return TinyHtml.setAttr(el, name, value.toString());
5885
+ }
5886
+ /**
5887
+ * Set a BigInt attribute.
5888
+ * @param {string} name
5889
+ * @param {bigint} value
5890
+ * @returns {this}
5891
+ */
5892
+ setAttrBigInt(name, value) {
5893
+ return TinyHtml.setAttrBigInt(this, name, value);
5894
+ }
5895
+ /**
5896
+ * Returns the Date value of an attribute.
5897
+ * @param {TinyElement} el
5898
+ * @param {string} name
5899
+ * @returns {Date|null}
5900
+ */
5901
+ static attrDate(el, name) {
5902
+ const elem = TinyHtml._preElem(el, 'attrDate');
5903
+ const val = elem.getAttribute(TinyHtml.getAttrName(name));
5904
+ if (!val)
5905
+ return null;
5906
+ const d = new Date(val);
5907
+ return Number.isNaN(d.getTime()) ? null : d;
5908
+ }
5909
+ /**
5910
+ * Returns the Date value of an attribute.
5911
+ * @param {string} name
5912
+ * @returns {Date|null}
5913
+ */
5914
+ attrDate(name) {
5915
+ return TinyHtml.attrDate(this, name);
5916
+ }
5917
+ /**
5918
+ * Set a Date attribute.
5919
+ * @template {TinyElement|TinyElement[]} T
5920
+ * @param {T} el
5921
+ * @param {string} name
5922
+ * @param {Date} value
5923
+ * @returns {T}
5924
+ */
5925
+ static setAttrDate(el, name, value) {
5926
+ if (!(value instanceof Date) || Number.isNaN(value.getTime()))
5927
+ throw new Error('Value is not a valid Date.');
5928
+ return TinyHtml.setAttr(el, name, value.toISOString());
5929
+ }
5930
+ /**
5931
+ * Set a Date attribute.
5932
+ * @param {string} name
5933
+ * @param {Date} value
5934
+ * @returns {this}
5935
+ */
5936
+ setAttrDate(name, value) {
5937
+ return TinyHtml.setAttrDate(this, name, value);
5938
+ }
5939
+ /**
5940
+ * Returns the JSON value of an attribute.
5941
+ * @param {TinyElement} el
5942
+ * @param {string} name
5943
+ * @returns {any|null}
5944
+ */
5945
+ static attrJson(el, name) {
5946
+ const elem = TinyHtml._preElem(el, 'attrJson');
5947
+ const val = elem.getAttribute(TinyHtml.getAttrName(name));
5948
+ if (!val)
5949
+ return null;
5950
+ try {
5951
+ return JSON.parse(val);
5952
+ }
5953
+ catch {
5954
+ return null;
5955
+ }
5956
+ }
5957
+ /**
5958
+ * Returns the JSON value of an attribute.
5959
+ * @param {string} name
5960
+ * @returns {any|null}
5961
+ */
5962
+ attrJson(name) {
5963
+ return TinyHtml.attrJson(this, name);
5964
+ }
5965
+ /**
5966
+ * Set a JSON attribute.
5967
+ * @template {TinyElement|TinyElement[]} T
5968
+ * @param {T} el
5969
+ * @param {string} name
5970
+ * @param {any} value
5971
+ * @param {(this: any, key: string, value: any) => any} [replacer]
5972
+ * @param {number|string} [space]
5973
+ * @returns {T}
5974
+ */
5975
+ static setAttrJson(el, name, value, replacer, space) {
5976
+ const data = JSON.stringify(value, replacer, space);
5977
+ return TinyHtml.setAttr(el, name, data);
5978
+ }
5979
+ /**
5980
+ * Set a JSON attribute.
5981
+ * @param {string} name
5982
+ * @param {any} value
5983
+ * @param {(this: any, key: string, value: any) => any} [replacer]
5984
+ * @param {number|string} [space]
5985
+ * @returns {this}
5986
+ */
5987
+ setAttrJson(name, value, replacer, space) {
5988
+ return TinyHtml.setAttrJson(this, name, value, replacer, space);
5989
+ }
5990
+ /**
5991
+ * Returns the number value of an attribute.
5992
+ * @param {TinyElement} el
5993
+ * @param {string} name
5994
+ * @returns {number|null}
5995
+ */
5996
+ static attrNumber(el, name) {
5997
+ const elem = TinyHtml._preElem(el, 'attrNumber');
5998
+ const val = elem.getAttribute(TinyHtml.getAttrName(name));
5999
+ return val != null ? parseFloat(val) : null;
6000
+ }
6001
+ /**
6002
+ * Returns the number value of an attribute.
6003
+ * @param {string} name
6004
+ * @returns {number|null}
6005
+ */
6006
+ attrNumber(name) {
6007
+ return TinyHtml.attrNumber(this, name);
6008
+ }
6009
+ /**
6010
+ * Set a number attribute.
6011
+ * @template {TinyElement|TinyElement[]} T
6012
+ * @param {T} el
6013
+ * @param {string} name
6014
+ * @param {number} value
6015
+ * @returns {T}
6016
+ */
6017
+ static setAttrNumber(el, name, value) {
6018
+ if (typeof value !== 'number')
6019
+ throw new Error('Value is not a valid number.');
6020
+ return TinyHtml.setAttr(el, name, value.toString());
6021
+ }
6022
+ /**
6023
+ * Set a number attribute.
6024
+ * @param {string} name
6025
+ * @param {number} value
6026
+ * @returns {this}
6027
+ */
6028
+ setAttrNumber(name, value) {
6029
+ return TinyHtml.setAttrNumber(this, name, value);
6030
+ }
6031
+ /**
6032
+ * Returns the boolean value of an attribute.
6033
+ * @param {TinyElement} el
6034
+ * @param {string} name
6035
+ * @returns {boolean|null}
6036
+ */
6037
+ static attrBoolean(el, name) {
6038
+ const elem = TinyHtml._preElem(el, 'attrBoolean');
6039
+ const val = elem.getAttribute(TinyHtml.getAttrName(name));
6040
+ if (val === null)
6041
+ return null;
6042
+ if (val !== 'true' && val !== 'false')
6043
+ return null;
6044
+ return /^true$/i.test(val);
6045
+ }
6046
+ /**
6047
+ * Returns the boolean value of an attribute.
6048
+ * @param {string} name
6049
+ * @returns {boolean|null}
6050
+ */
6051
+ attrBoolean(name) {
6052
+ return TinyHtml.attrBoolean(this, name);
6053
+ }
6054
+ /**
6055
+ * Set a boolean attribute.
6056
+ * @template {TinyElement|TinyElement[]} T
6057
+ * @param {T} el
6058
+ * @param {string} name
6059
+ * @param {boolean} value
6060
+ * @returns {T}
6061
+ */
6062
+ static setAttrBoolean(el, name, value) {
6063
+ if (typeof value !== 'boolean')
6064
+ throw new Error('Value is not a valid boolean.');
6065
+ return TinyHtml.setAttr(el, name, value.toString());
6066
+ }
6067
+ /**
6068
+ * Set a boolean attribute.
6069
+ * @param {string} name
6070
+ * @param {boolean} value
6071
+ * @returns {this}
6072
+ */
6073
+ setAttrBoolean(name, value) {
6074
+ return TinyHtml.setAttrBoolean(this, name, value);
6075
+ }
6076
+ /**
6077
+ * Returns the string value of an attribute.
6078
+ * @param {TinyElement} el
6079
+ * @param {string} name
6080
+ * @returns {string|null}
6081
+ */
6082
+ static attrString(el, name) {
6083
+ const elem = TinyHtml._preElem(el, 'attrString');
6084
+ const value = elem.getAttribute(TinyHtml.getAttrName(name));
6085
+ return typeof value === 'string' ? value : null;
6086
+ }
6087
+ /**
6088
+ * Returns the string value of an attribute.
6089
+ * @param {string} name
6090
+ * @returns {string|null}
6091
+ */
6092
+ attrString(name) {
6093
+ return TinyHtml.attrString(this, name);
6094
+ }
6095
+ /**
6096
+ * Set a string attribute.
6097
+ * @template {TinyElement|TinyElement[]} T
6098
+ * @param {T} el
6099
+ * @param {string} name
6100
+ * @param {string} value
6101
+ * @returns {T}
6102
+ */
6103
+ static setAttrString(el, name, value) {
6104
+ if (typeof value !== 'string')
6105
+ throw new Error('Value is not a valid string.');
6106
+ return TinyHtml.setAttr(el, name, value);
6107
+ }
6108
+ /**
6109
+ * Set a string attribute.
6110
+ * @param {string} name
6111
+ * @param {string} value
6112
+ * @returns {this}
6113
+ */
6114
+ setAttrString(name, value) {
6115
+ return TinyHtml.setAttrString(this, name, value);
6116
+ }
5846
6117
  /**
5847
6118
  * Get an attribute on an element.
5848
6119
  * @param {TinyElement} el
@@ -5867,43 +6138,37 @@ class TinyHtml {
5867
6138
  * Set one or multiple attributes on an element.
5868
6139
  * @template {TinyElement|TinyElement[]} T
5869
6140
  * @param {T} el - Target element(s).
5870
- * @param {string|Record<string, string|null>} name - Attribute name or an object of attributes.
5871
- * @param {string|number|null} [value=null] - Attribute value (ignored if "name" is an object).
6141
+ * @param {string|Record<string, any>} name - Attribute name or an object of attributes.
6142
+ * @param {any} [value=null] - Attribute value (ignored if "name" is an object).
5872
6143
  * @returns {T}
5873
6144
  */
5874
6145
  static setAttr(el, name, value = null) {
5875
6146
  const elems = TinyHtml._preElems(el, 'setAttr');
5876
6147
  // Multiple attributes at once
5877
6148
  if (typeof name === 'object' && name !== null) {
5878
- Object.entries(name).forEach(([attr, val]) => {
5879
- if (val !== null && typeof val !== 'string' && typeof val !== 'number')
5880
- throw new TypeError(`The value for "${attr}" must be a string/number or null.`);
5881
- elems.forEach((elem) => {
5882
- if (val === null)
5883
- elem.removeAttribute(TinyHtml.getAttrName(attr));
5884
- else
5885
- elem.setAttribute(TinyHtml.getAttrName(attr), val);
5886
- });
5887
- });
6149
+ Object.entries(name).forEach(([attr, val]) => elems.forEach((elem) => {
6150
+ if (val === null)
6151
+ elem.removeAttribute(TinyHtml.getAttrName(attr));
6152
+ else
6153
+ elem.setAttribute(TinyHtml.getAttrName(attr), String(val));
6154
+ }));
5888
6155
  return el;
5889
6156
  }
5890
6157
  // Single attribute
5891
6158
  if (typeof name !== 'string')
5892
6159
  throw new TypeError('The "name" must be a string.');
5893
- if (value !== null && typeof value !== 'string')
5894
- throw new TypeError('The "value" must be a string.');
5895
6160
  elems.forEach((elem) => {
5896
6161
  if (value === null)
5897
6162
  elem.removeAttribute(TinyHtml.getAttrName(name));
5898
6163
  else
5899
- elem.setAttribute(TinyHtml.getAttrName(name), value);
6164
+ elem.setAttribute(TinyHtml.getAttrName(name), String(value));
5900
6165
  });
5901
6166
  return el;
5902
6167
  }
5903
6168
  /**
5904
6169
  * Set one or multiple attributes on an element.
5905
- * @param {string|Record<string, string|null>} name - Attribute name or an object of attributes.
5906
- * @param {string|number|null} [value=null] - Attribute value (ignored if "name" is an object).
6170
+ * @param {string|Record<string, any>} name - Attribute name or an object of attributes.
6171
+ * @param {any} [value=null] - Attribute value (ignored if "name" is an object).
5907
6172
  * @returns {this}
5908
6173
  */
5909
6174
  setAttr(name, value) {
@@ -2076,6 +2076,42 @@ element.removeAttr("data-test"); // Remove
2076
2076
  element.hasAttr("id"); // Check
2077
2077
  ```
2078
2078
 
2079
+ ```js
2080
+ element.attrBigInt("example"); // Get
2081
+ element.setAttrBigInt("example", 10n); // Set
2082
+ element.setAttrBigInt({ "example": 10n }); // Set
2083
+ ```
2084
+
2085
+ ```js
2086
+ element.attrDate("example"); // Get
2087
+ element.setAttrDate("example", new Date()); // Set
2088
+ element.setAttrDate({ "example": new Date() }); // Set
2089
+ ```
2090
+
2091
+ ```js
2092
+ element.attrJson("example"); // Get
2093
+ element.setAttrJson("example", { pudding: true }); // Set
2094
+ element.setAttrJson({ "example": { pudding: true } }); // Set
2095
+ ```
2096
+
2097
+ ```js
2098
+ element.attrNumber("example"); // Get
2099
+ element.setAttrNumber("example", 1); // Set
2100
+ element.setAttrNumber({ "example": 1 }); // Set
2101
+ ```
2102
+
2103
+ ```js
2104
+ element.attrBoolean("example"); // Get
2105
+ element.setAttrBoolean("example", true); // Set
2106
+ element.setAttrBoolean({ "example": true }); // Set
2107
+ ```
2108
+
2109
+ ```js
2110
+ element.attrString("example"); // Get
2111
+ element.setAttrString("example", "pudding"); // Set
2112
+ element.setAttrString({ "example": "pudding" }); // Set
2113
+ ```
2114
+
2079
2115
  Works on both single and multiple elements (static or instance).
2080
2116
  Safe and type-checked — throws if misuse is detected.
2081
2117
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tiny-essentials",
3
- "version": "1.22.4",
3
+ "version": "1.22.5",
4
4
  "description": "Collection of small, essential scripts designed to be used across various projects. These simple utilities are crafted for speed, ease of use, and versatility.",
5
5
  "scripts": {
6
6
  "test": "npm run test:mjs && npm run test:cjs && npm run test:js",