@flatfile/hooks
Advanced tools
Comparing version 1.3.2 to 1.4.0
@@ -27,2 +27,3 @@ type TPrimitive = string | boolean | number | null; | ||
info: IRecordInfo<M>[]; | ||
config?: Record<string, any>; | ||
} | ||
@@ -46,2 +47,3 @@ interface IPayload { | ||
private _info; | ||
_config: Record<string, any>; | ||
constructor(raw: IRawRecord); | ||
@@ -51,2 +53,32 @@ get rowId(): string | number; | ||
get value(): M; | ||
/** | ||
* Make the entire record or specific fields readonly by a user. | ||
* | ||
* @example ```js | ||
* record.setReadOnly() // make entire record readonly | ||
* ``` | ||
* | ||
* @example ```js | ||
* record.setReadOnly('firstname', 'lastname') // make two fields readonly | ||
* ``` | ||
* @param fields | ||
*/ | ||
setReadOnly(...fields: string[]): void; | ||
/** | ||
* Make the entire record or specific fields writable by a user. | ||
* | ||
* @example ```js | ||
* record.setWritable() // make entire record writable | ||
* ``` | ||
* | ||
* @example ```js | ||
* record.setWritable('firstname', 'lastname') // make two fields writable | ||
* ``` | ||
* @param fields | ||
*/ | ||
setWritable(...fields: string[]): void; | ||
/** | ||
* Return the current state of the record as simple key value pairs. | ||
*/ | ||
get obj(): Record<string, TPrimitive>; | ||
private verifyField; | ||
@@ -53,0 +85,0 @@ private isLinkedField; |
@@ -29,2 +29,18 @@ "use strict"; | ||
// src/classes/utils.ts | ||
function digSet(obj, value, ...keys) { | ||
const base = obj || {}; | ||
let current = base; | ||
for (let i = 0; i < keys.length - 1; i++) { | ||
const key = keys[i]; | ||
if (!current[key] || typeof current[key] !== "object") { | ||
current[key] = {}; | ||
} | ||
current = current[key]; | ||
} | ||
const finalKey = keys[keys.length - 1]; | ||
current[finalKey] = value; | ||
return base; | ||
} | ||
// src/classes/FlatfileRecord.ts | ||
@@ -35,2 +51,3 @@ var propExists = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop); | ||
this._info = []; | ||
this._config = {}; | ||
this.mutated = Object.assign({}, raw.rawData); | ||
@@ -50,9 +67,56 @@ this.data = Object.assign({}, raw.rawData); | ||
} | ||
verifyField(field, data) { | ||
if (!propExists(data || this.data, field)) { | ||
console.error(`Record does not have field "${field}".`); | ||
return false; | ||
/** | ||
* Make the entire record or specific fields readonly by a user. | ||
* | ||
* @example ```js | ||
* record.setReadOnly() // make entire record readonly | ||
* ``` | ||
* | ||
* @example ```js | ||
* record.setReadOnly('firstname', 'lastname') // make two fields readonly | ||
* ``` | ||
* @param fields | ||
*/ | ||
setReadOnly(...fields) { | ||
if (!fields.length) { | ||
this._config = digSet(this._config, true, "readonly"); | ||
} | ||
return true; | ||
fields.forEach((field) => { | ||
this._config = digSet(this._config, true, "fields", field, "readonly"); | ||
}); | ||
} | ||
/** | ||
* Make the entire record or specific fields writable by a user. | ||
* | ||
* @example ```js | ||
* record.setWritable() // make entire record writable | ||
* ``` | ||
* | ||
* @example ```js | ||
* record.setWritable('firstname', 'lastname') // make two fields writable | ||
* ``` | ||
* @param fields | ||
*/ | ||
setWritable(...fields) { | ||
if (!fields.length) { | ||
this._config = digSet(this._config, false, "readonly"); | ||
} | ||
fields.forEach((field) => { | ||
this._config = digSet(this._config, false, "fields", field, "readonly"); | ||
}); | ||
} | ||
/** | ||
* Return the current state of the record as simple key value pairs. | ||
*/ | ||
get obj() { | ||
return Object.fromEntries( | ||
Object.entries(this.mutated).map(([key, value]) => [ | ||
key, | ||
typeof value === "object" && value && "value" in value ? value.value : value | ||
]) | ||
); | ||
} | ||
verifyField(field, data) { | ||
return propExists(data || this.data, field); | ||
} | ||
isLinkedField(field) { | ||
@@ -64,2 +128,3 @@ const fieldValue = this.mutated[field]; | ||
if (!this.verifyField(field)) { | ||
console.error(`Record does not have field "${field}".`); | ||
return this; | ||
@@ -185,2 +250,3 @@ } | ||
}, | ||
config: this._config, | ||
info: this._info | ||
@@ -187,0 +253,0 @@ }; |
{ | ||
"name": "@flatfile/hooks", | ||
"version": "1.3.2", | ||
"version": "1.4.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.js", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
23068
698