@valkyriestudios/utils 12.22.0 → 12.23.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.
package/README.md CHANGED
@@ -787,7 +787,7 @@ isFormData(new FormData()); // TRUE
787
787
  isFormData({hi: 'there'}); // FALSE
788
788
  ```
789
789
 
790
- ### formdata/toObject(val:FormData, {raw?:string[]} = {})
790
+ ### formdata/toObject(val:FormData, {raw?:string[]|true;single?:string[]} = {})
791
791
  Converts an instance of FormData to an object
792
792
  ```typescript
793
793
  import toObject from '@valkyriestudios/utils/formdata/toObject';
@@ -840,6 +840,19 @@ toObject(form, {raw: ['pincode']}); /* {
840
840
  } */
841
841
  ```
842
842
 
843
+ Take Note: Set raw to `true` to do no normalization
844
+
845
+ Allows passing a 'single' list that tells the system to NEVER turn a particular value into an array of values:
846
+ ```typescript
847
+ const formData = new FormData();
848
+ formData.append('status', 'active');
849
+ formData.append('status', 'inactive');
850
+ formData.append('action', 'save');
851
+ formData.append('action', 'reset');
852
+
853
+ toObject(formData, { single: ['status', 'action'] }) /* {status: 'inactive', action: 'reset'} */
854
+ ```
855
+
843
856
  ### hash/guid()
844
857
  Generate a unique identifier (guid) according to RFC4122
845
858
  ```typescript
@@ -1239,4 +1252,4 @@ humanizeNumber(47328748923747923479); // '47,328.75q'
1239
1252
 
1240
1253
  ## Contributors
1241
1254
  - [Peter Vermeulen](https://www.linkedin.com/in/petervermeulen1/)
1242
- - [Xander Berkein](https://github.com/xanderberkein)
1255
+ - [Xander Berkein](https://github.com/xanderberkein)
@@ -2,7 +2,11 @@ type toObjectConfig = {
2
2
  /**
3
3
  * Pass array of keys that should not be normalized into number/bool when seen
4
4
  */
5
- raw?: string[];
5
+ raw?: string[] | true;
6
+ /**
7
+ * Pass array of keys that should only have a single value (e.g., 'action')
8
+ */
9
+ single?: string[];
6
10
  };
7
11
  /**
8
12
  * Converts a FormData instance to a json object
@@ -3,13 +3,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.toObject = toObject;
4
4
  exports.default = toObject;
5
5
  const RGX_CLOSE = /\]/g;
6
- function assignValue(acc, rawkey, value) {
6
+ function assignValue(acc, rawkey, value, single) {
7
7
  let cursor = acc;
8
8
  const keys = rawkey.replace(RGX_CLOSE, '').split(/\[|\./);
9
9
  for (let i = 0; i < keys.length; i++) {
10
10
  const key = keys[i];
11
11
  if (i === keys.length - 1) {
12
- if (cursor[key] !== undefined) {
12
+ if (cursor[key] !== undefined && (!single || !single.has(key))) {
13
13
  if (Array.isArray(cursor[key])) {
14
14
  cursor[key].push(value);
15
15
  }
@@ -38,11 +38,12 @@ function assignValue(acc, rawkey, value) {
38
38
  function toObject(form, config) {
39
39
  if (!(form instanceof FormData))
40
40
  throw new Error('formdata/toObject: Value is not an instance of FormData');
41
- const set = new Set(Array.isArray(config?.raw) ? config.raw : []);
41
+ const set = config?.raw === true ? true : new Set(Array.isArray(config?.raw) ? config?.raw : []);
42
+ const single = Array.isArray(config?.single) && config?.single.length ? new Set(config.single) : null;
42
43
  const acc = {};
43
44
  form.forEach((value, key) => {
44
45
  let normalizedValue = value;
45
- if (typeof value === 'string' && !set.has(key)) {
46
+ if (set !== true && typeof value === 'string' && !set.has(key)) {
46
47
  const lower = value.toLowerCase();
47
48
  normalizedValue = (lower === 'true'
48
49
  ? true
@@ -52,7 +53,7 @@ function toObject(form, config) {
52
53
  ? Number(value)
53
54
  : value);
54
55
  }
55
- assignValue(acc, key, normalizedValue);
56
+ assignValue(acc, key, normalizedValue, single);
56
57
  });
57
58
  return acc;
58
59
  }
package/hash/fnv1A.d.ts CHANGED
@@ -1,3 +1,5 @@
1
+ export declare const FNV_32 = 2166136261;
2
+ export declare const FNV_64 = 1099511628211;
1
3
  /**
2
4
  * Convert a provided value into a Fowler-Noll-Vo 1A hash
3
5
  * For more info: https://tools.ietf.org/html/draft-eastlake-fnv-03
package/hash/fnv1A.js CHANGED
@@ -1,14 +1,16 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FNV_64 = exports.FNV_32 = void 0;
3
4
  exports.fnv1A = fnv1A;
4
5
  exports.default = fnv1A;
5
- const FNV_32 = 2166136261;
6
+ exports.FNV_32 = 2166136261;
7
+ exports.FNV_64 = 1099511628211;
6
8
  const REPL_NAN = 'nan';
7
9
  const REPL_TRUE = 'true';
8
10
  const REPL_FALSE = 'false';
9
11
  const REPL_UNDEF = 'undefined';
10
12
  const REPL_NULL = 'null';
11
- function fnv1A(data, offset = FNV_32) {
13
+ function fnv1A(data, offset = exports.FNV_32) {
12
14
  let hash = offset;
13
15
  let sanitized;
14
16
  switch (typeof data) {
package/index.d.ts CHANGED
@@ -227,7 +227,8 @@ declare module "formdata/is" {
227
227
  }
228
228
  declare module "formdata/toObject" {
229
229
  type toObjectConfig = {
230
- raw?: string[];
230
+ raw?: string[] | true;
231
+ single?: string[];
231
232
  };
232
233
  function toObject<T extends Record<string, unknown>>(form: FormData, config?: toObjectConfig): T;
233
234
  export { toObject, toObject as default };
@@ -569,6 +570,8 @@ declare module "deep/index" {
569
570
  export { deepFreeze as freeze, deepFreeze, deepGet as get, deepGet, deepSeal as seal, deepSeal, deepSet as set, deepSet };
570
571
  }
571
572
  declare module "hash/fnv1A" {
573
+ export const FNV_32 = 2166136261;
574
+ export const FNV_64 = 1099511628211;
572
575
  function fnv1A(data: unknown, offset?: number): number;
573
576
  export { fnv1A, fnv1A as default };
574
577
  }
package/package.json CHANGED
@@ -1 +1 @@
1
- { "name": "@valkyriestudios/utils", "version": "12.22.0", "description": "A collection of single-function utilities for common tasks", "author": { "name": "Peter Vermeulen", "url": "https://www.linkedin.com/in/petervermeulen1/" }, "keywords": [ "utility", "library", "javascript", "js", "node", "bun" ], "license": "MIT", "repository": { "type": "git", "url": "git+https://github.com/ValkyrieStudios/utils.git" }, "bugs": { "url": "https://github.com/ValkyrieStudios/utils/issues" }, "homepage": "https://github.com/ValkyrieStudios/utils#readme", "types": "index.d.ts" }
1
+ { "name": "@valkyriestudios/utils", "version": "12.23.0", "description": "A collection of single-function utilities for common tasks", "author": { "name": "Peter Vermeulen", "url": "https://www.linkedin.com/in/petervermeulen1/" }, "keywords": [ "utility", "library", "javascript", "js", "node", "bun" ], "license": "MIT", "repository": { "type": "git", "url": "git+https://github.com/ValkyrieStudios/utils.git" }, "bugs": { "url": "https://github.com/ValkyrieStudios/utils/issues" }, "homepage": "https://github.com/ValkyrieStudios/utils#readme", "types": "index.d.ts" }