@acodeninja/persist 2.2.1 → 2.2.2

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.
@@ -1,15 +1,49 @@
1
1
  import Type from '../Type.js';
2
2
  import ajv from 'ajv';
3
3
 
4
- export default class CustomType {
4
+ /**
5
+ * Represents a custom type definition, allowing the specification of a type based on a given schema.
6
+ * This class uses AJV (Another JSON Schema Validator) to validate schemas and create type definitions.
7
+ *
8
+ * @class CustomType
9
+ */
10
+ class CustomType {
11
+ /**
12
+ * Creates a new custom type definition based on the provided JSON schema.
13
+ *
14
+ * The `of` method allows defining a custom object type using a JSON schema. It validates the schema
15
+ * using AJV to ensure correctness and returns a class representing the custom type.
16
+ *
17
+ * @param {Object} schema - The JSON schema that defines the structure and validation rules for the custom type.
18
+ * @returns {Type} A new class representing the custom type based on the provided schema.
19
+ *
20
+ * @example
21
+ * const customSchema = {
22
+ * type: 'object',
23
+ * properties: { name: { type: 'string' }, age: { type: 'number' } },
24
+ * required: ['name'],
25
+ * };
26
+ * const CustomModel = CustomType.of(customSchema);
27
+ */
5
28
  static of(schema) {
29
+ // Compiles and validates the schema using AJV
6
30
  new ajv().compile(schema);
7
31
 
32
+ /**
33
+ * @class Custom
34
+ * @extends Type
35
+ * Represents a custom type defined by a JSON schema.
36
+ */
8
37
  class Custom extends Type {
38
+ /** @type {string} The data type, which is 'object' */
9
39
  static _type = 'object';
40
+
41
+ /** @type {Object} The JSON schema that defines the structure and validation rules */
10
42
  static _schema = schema;
11
43
  }
12
44
 
13
45
  return Custom;
14
46
  }
15
47
  }
48
+
49
+ export default CustomType;
@@ -1,14 +1,50 @@
1
1
  import Type from '../Type.js';
2
2
 
3
- export default class ResolvedType extends Type {
3
+ /**
4
+ * Class representing a resolved type.
5
+ *
6
+ * The `ResolvedType` class extends the base `Type` class and marks the type as resolved.
7
+ * It provides additional functionality for resolving types and allows creating resolved types based on properties.
8
+ *
9
+ * @class ResolvedType
10
+ * @extends Type
11
+ */
12
+ class ResolvedType extends Type {
13
+ /**
14
+ * @static
15
+ * @property {boolean} _resolved - Indicates if the type is resolved. Always set to `true` for this class.
16
+ */
4
17
  static _resolved = true;
5
18
 
19
+ /**
20
+ * Resolves the type based on the provided model.
21
+ *
22
+ * This method should be overridden in subclasses to provide specific resolution logic.
23
+ * Throws an error if not implemented.
24
+ *
25
+ * @param {*} _model - The model used to resolve the type.
26
+ * @throws {Error} If the method is not implemented in a subclass.
27
+ */
6
28
  static resolve(_model) {
7
29
  throw new Error(`${this.name} does not implement resolve(model)`);
8
30
  }
9
31
 
32
+ /**
33
+ * Creates a subclass of `ResolvedType` that is based on the provided property.
34
+ *
35
+ * The returned class inherits from `ResolvedType` and customizes its `toString` method
36
+ * to reflect the resolved property.
37
+ *
38
+ * @param {*} property - The property to base the resolved type on.
39
+ * @returns {ResolvedType} A subclass of `ResolvedType` customized for the provided property.
40
+ */
10
41
  static of(property) {
11
42
  class ResolvedTypeOf extends ResolvedType {
43
+ /**
44
+ * Converts the resolved type to a string, displaying the resolved property.
45
+ *
46
+ * @returns {string} A string representing the resolved type, including the property.
47
+ */
12
48
  static toString() {
13
49
  return `ResolvedTypeOf(${property})`;
14
50
  }
@@ -17,3 +53,5 @@ export default class ResolvedType extends Type {
17
53
  return ResolvedTypeOf;
18
54
  }
19
55
  }
56
+
57
+ export default ResolvedType;
@@ -1,15 +1,53 @@
1
1
  import ResolvedType from './ResolvedType.js';
2
2
  import slugify from 'slugify';
3
3
 
4
- export default class SlugType extends ResolvedType {
4
+ /**
5
+ * Class representing a slug type.
6
+ *
7
+ * The `SlugType` class extends the `ResolvedType` and provides functionality for generating
8
+ * slugified strings based on a specified property of a model. It allows for the creation of
9
+ * types that resolve into slugs from specific properties.
10
+ *
11
+ * @class SlugType
12
+ * @extends ResolvedType
13
+ */
14
+ class SlugType extends ResolvedType {
15
+ /**
16
+ * Creates a subclass of `ResolvedType` that generates slugs based on the provided property.
17
+ *
18
+ * The returned class inherits from `ResolvedType` and resolves the property into a slugified
19
+ * string using the `slugify` function. It also customizes the `toString` method to reflect
20
+ * the resolved property.
21
+ *
22
+ * @param {string} property - The property to base the slug on.
23
+ * @returns {ResolvedType} A subclass of `ResolvedType` that generates slugs from the provided property.
24
+ */
5
25
  static of(property) {
6
26
  class SlugOf extends ResolvedType {
27
+ /**
28
+ * @static
29
+ * @property {string} _type - The type of the resolved value, always set to 'string' for slug types.
30
+ */
7
31
  static _type = 'string';
8
32
 
33
+ /**
34
+ * Converts the slug type to a string, displaying the resolved property.
35
+ *
36
+ * @returns {string} A string representing the slug type, including the property.
37
+ */
9
38
  static toString() {
10
39
  return `SlugOf(${property})`;
11
40
  }
12
41
 
42
+ /**
43
+ * Resolves the slug from the given model by extracting the specified property and slugifying it.
44
+ *
45
+ * If the specified property in the model is not a string, an empty string is returned.
46
+ * Uses the `slugify` function to convert the property value into a slug (lowercase, hyphen-separated).
47
+ *
48
+ * @param {Object} model - The model from which to extract and slugify the property.
49
+ * @returns {string} The slugified version of the model's property, or an empty string if not valid.
50
+ */
13
51
  static resolve(model) {
14
52
  if (typeof model?.[property] !== 'string') return '';
15
53
 
@@ -22,3 +60,5 @@ export default class SlugType extends ResolvedType {
22
60
  return SlugOf;
23
61
  }
24
62
  }
63
+
64
+ export default SlugType;
@@ -1,5 +1,20 @@
1
1
  import SimpleType from './SimpleType.js';
2
2
 
3
- export default class BooleanType extends SimpleType {
3
+ /**
4
+ * Class representing a boolean type.
5
+ *
6
+ * This class is used to define and handle data of the boolean type.
7
+ * It extends the {@link SimpleType} class to represent string-specific behavior.
8
+ *
9
+ * @class BooleanType
10
+ * @extends SimpleType
11
+ */
12
+ class BooleanType extends SimpleType {
13
+ /**
14
+ * @static
15
+ * @property {string} _type - The type identifier for BooleanType, set to `'boolean'`.
16
+ */
4
17
  static _type = 'boolean';
5
18
  }
19
+
20
+ export default BooleanType;
@@ -1,10 +1,37 @@
1
1
  import SimpleType from './SimpleType.js';
2
2
 
3
- export default class DateType extends SimpleType {
3
+ /**
4
+ * Class representing a date type with ISO date-time format.
5
+ *
6
+ * This class is used to define and handle data of the date type.
7
+ * It extends the {@link SimpleType} class to represent string-specific behavior.
8
+ *
9
+ * @class DateType
10
+ * @extends SimpleType
11
+ */
12
+ class DateType extends SimpleType {
13
+ /**
14
+ * @static
15
+ * @property {string} _type - The type identifier for DateType, set to `'string'`.
16
+ */
4
17
  static _type = 'string';
18
+
19
+ /**
20
+ * @static
21
+ * @property {string} _format - The format for DateType, set to `'iso-date-time'`.
22
+ */
5
23
  static _format = 'iso-date-time';
6
24
 
25
+ /**
26
+ * Checks if the given value is a valid date.
27
+ *
28
+ * @static
29
+ * @param {*} possibleDate - The value to check for a valid date.
30
+ * @returns {boolean} Returns `true` if the value is a valid date or a date string, otherwise `false`.
31
+ */
7
32
  static isDate(possibleDate) {
8
33
  return possibleDate instanceof Date || !isNaN(new Date(possibleDate));
9
34
  }
10
35
  }
36
+
37
+ export default DateType;
@@ -1,5 +1,20 @@
1
1
  import SimpleType from './SimpleType.js';
2
2
 
3
- export default class NumberType extends SimpleType {
3
+ /**
4
+ * Class representing a number type.
5
+ *
6
+ * This class is used to define and handle data of the number type.
7
+ * It extends the {@link SimpleType} class to represent string-specific behavior.
8
+ *
9
+ * @class NumberType
10
+ * @extends SimpleType
11
+ */
12
+ class NumberType extends SimpleType {
13
+ /**
14
+ * @static
15
+ * @property {string} _type - The type identifier for NumberType, set to `'number'`.
16
+ */
4
17
  static _type = 'number';
5
18
  }
19
+
20
+ export default NumberType;
@@ -1,4 +1,14 @@
1
1
  import Type from '../Type.js';
2
2
 
3
- export default class SimpleType extends Type {
3
+ /**
4
+ * Class representing a simple type.
5
+ *
6
+ * This serves as a base class for primitive or simple types such as string, number, or boolean.
7
+ *
8
+ * @class SimpleType
9
+ * @extends Type
10
+ */
11
+ class SimpleType extends Type {
4
12
  }
13
+
14
+ export default SimpleType;
@@ -1,5 +1,20 @@
1
1
  import SimpleType from './SimpleType.js';
2
2
 
3
- export default class StringType extends SimpleType {
3
+ /**
4
+ * Class representing a string type.
5
+ *
6
+ * This class is used to define and handle data of the string type.
7
+ * It extends the {@link SimpleType} class to represent string-specific behavior.
8
+ *
9
+ * @class StringType
10
+ * @extends SimpleType
11
+ */
12
+ class StringType extends SimpleType {
13
+ /**
14
+ * @static
15
+ * @property {string} _type - The type identifier for the string type.
16
+ */
4
17
  static _type = 'string';
5
18
  }
19
+
20
+ export default StringType;