Skip to content

Commit

Permalink
Merge pull request #231 from trojs/feature/update-20240725
Browse files Browse the repository at this point in the history
Update linting
  • Loading branch information
w3nl authored Jul 25, 2024
2 parents 1addb68 + 2e2a62a commit 3690af9
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 696 deletions.
10 changes: 8 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
"import"
],
"extends": [
"@hckrnews/eslint-config"
"@hckrnews/eslint-config",
"plugin:import/recommended",
"plugin:n/recommended",
"plugin:jsdoc/recommended"
],
"rules": {
"import/extensions": [
Expand All @@ -18,7 +21,10 @@
{
"js": "always"
}
]
],
"jsdoc/require-param-description": "off",
"jsdoc/require-property-description": "off",
"jsdoc/require-returns-description": "off"
},
"parserOptions": {
"sourceType": "module",
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:

strategy:
matrix:
node-version: [18.x, 20.x, 21.x, 22.x]
node-version: [18.x, 20.x, 22.x]

steps:
- uses: actions/checkout@v4
Expand Down
738 changes: 134 additions & 604 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@trojs/objects",
"description": "Create valid JavaScript objects",
"version": "8.0.4",
"version": "8.0.5",
"author": {
"name": "Pieter Wigboldus",
"url": "https://trojs.org/"
Expand Down Expand Up @@ -36,8 +36,11 @@
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-html": "^8.0.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-jsdoc": "^48.8.3",
"eslint-plugin-jsx-a11y": "^6.6.0",
"eslint-plugin-n": "^17.0.0",
"eslint-plugin-prettier": "^5.1.3",
"eslint-plugin-promise": "^7.0.0",
"jscpd": "^4.0.0",
"prettier": "^3.0.0"
},
Expand Down
2 changes: 0 additions & 2 deletions src/int.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ export default class Int extends Number {
/**
* Constructor that receive the value,
* and pass it to the number constructor.
*
* @param {number} value
*/
constructor(value) {
Expand All @@ -16,7 +15,6 @@ export default class Int extends Number {

/**
* Validate the value, it should throw an error if it isnt a integer.
*
* @param {number} value
*/
validate(value) {
Expand Down
104 changes: 30 additions & 74 deletions src/objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,14 @@ const dataToString = ({ data, parentData, field, parentField }) => {

/**
* Object helper
*
* @param {object} schema
*
* @return {class}
* @param {object} params
* @param {object=} params.schema
* @returns {Obj}

Check warning on line 59 in src/objects.js

View workflow job for this annotation

GitHub Actions / build

The type 'Obj' is undefined
*/
const ObjectGenerator = ({ schema } = {}) =>
class Obj {
/**
* Set the original and prefix.
*
* @param {object} original
* @param {string=} prefix
*/
Expand All @@ -79,8 +77,7 @@ const ObjectGenerator = ({ schema } = {}) =>

/**
* Get the sub schema, look e.g. for optional fields.
*
* @return {object}
* @returns {object}
*/
get subSchema() {
if (!this.prefix) {
Expand Down Expand Up @@ -117,8 +114,7 @@ const ObjectGenerator = ({ schema } = {}) =>

/**
* Setup the validator with the subschema.
*
* @return {object|null}
* @returns {object|null}
*/
get validator() {
return this.subSchema && this.subSchema.constructor === Object
Expand All @@ -128,8 +124,7 @@ const ObjectGenerator = ({ schema } = {}) =>

/**
* Get the original data as array.
*
* @return {array}
* @returns {Array}
*/
get originalData() {
return this.original?.constructor === Array
Expand Down Expand Up @@ -251,56 +246,49 @@ const ObjectGenerator = ({ schema } = {}) =>

/**
* Get the flat object.
*
* @return {object}
* @returns {object}
*/
get flat() {
return this.flatObject;
}

/**
* Get the object entries.
*
* @return {array}
* @returns {Array}
*/
entries() {
return Object.entries(this.flatObject);
}

/**
* Get the object keys.
*
* @return {array}
* @returns {Array}
*/
keys() {
return Object.keys(this.flatObject);
}

/**
* Get the object values.
*
* @return {array}
* @returns {Array}
*/
values() {
return Object.values(this.flatObject);
}

/**
* Get the object length.
*
* @return {number}
* @returns {number}
*/
get length() {
return Object.keys(this.flatObject).length;
}

/**
* Get an item by key.
*
* @param {string} key
* @param {object|null} defaultValue
*
* @return {object|null}
* @returns {object|null}
*/
getByKey(key, defaultValue) {
if (this.originalHas(key)) {
Expand All @@ -327,11 +315,9 @@ const ObjectGenerator = ({ schema } = {}) =>

/**
* Get keys of an item.
*
* @param {array} keys
* @param {Array} keys
* @param {object|null} defaultValue
*
* @return {object|null}
* @returns {object|null}
*/
getFlatKeys(keys, defaultValue) {
const result = this.entries().filter(([currentKey]) =>
Expand All @@ -347,11 +333,9 @@ const ObjectGenerator = ({ schema } = {}) =>

/**
* Get keys of an item.
*
* @param {array} keys
* @param {Array} keys
* @param {object|null} defaultValue
*
* @return {object|null}
* @returns {object|null}
*/
getKeys(keys, defaultValue) {
const result = keys.reduce((accumulator, currentKey) => {
Expand All @@ -374,32 +358,26 @@ const ObjectGenerator = ({ schema } = {}) =>

/**
* Check if the original object has a key.
*
* @param {string} key
*
* @return {boolean}
* @returns {boolean}
*/
originalHas(key) {
return key in this.original;
}

/**
* Check if the object has a key.
*
* @param {string} key
*
* @return {boolean}
* @returns {boolean}
*/
has(key) {
return key in this.flatObject;
}

/**
* Check if the object has a key that includes.
*
* @param {string} key
*
* @return {boolean}
* @returns {boolean}
*/
includes(key) {
return (
Expand All @@ -410,10 +388,8 @@ const ObjectGenerator = ({ schema } = {}) =>
/**
* The map() method creates a new object populated with the results of
* calling a provided function on every element in the original object.
*
* @param {Function} callbackFunction
*
* @return {object}
* @returns {object}
*/
map(callbackFunction) {
return Object.fromEntries(
Expand All @@ -427,10 +403,8 @@ const ObjectGenerator = ({ schema } = {}) =>
/**
* The filter() method creates a new object with all elements
* that pass the test implemented by the provided function.
*
* @param {Function} callbackFunction
*
* @return {object}
* @returns {object}
*/
filter(callbackFunction) {
return Object.fromEntries(
Expand All @@ -445,10 +419,8 @@ const ObjectGenerator = ({ schema } = {}) =>
* The every() method tests whether all elements in the object pass the
* test implemented by the provided function.
* It returns a Boolean value.
*
* @param {Function} callbackFunction
*
* @return {boolean}
* @returns {boolean}
*/
every(callbackFunction) {
return Object.values(this.original).every((value) =>
Expand All @@ -462,10 +434,8 @@ const ObjectGenerator = ({ schema } = {}) =>
* It returns true if, in the object, it finds an element for which the
* provided function returns true; otherwise it returns false.
* It doesn't modify the object.
*
* @param {Function} callbackFunction
*
* @return {boolean}
* @returns {boolean}
*/
some(callbackFunction) {
return Object.values(this.original).some((value) =>
Expand All @@ -476,10 +446,8 @@ const ObjectGenerator = ({ schema } = {}) =>
/**
* The flatMap() method creates a new object populated with the results
* of calling a provided function on every element in the flat object.
*
* @param {Function} callbackFunction
*
* @return {object}
* @returns {object}
*/
flatMap(callbackFunction) {
return Object.fromEntries(
Expand All @@ -493,10 +461,8 @@ const ObjectGenerator = ({ schema } = {}) =>
/**
* The flatFilter() method creates a new object with all elements
* that pass the test implemented by the provided function.
*
* @param {Function} callbackFunction
*
* @return {object}
* @returns {object}
*/
flatFilter(callbackFunction) {
return Object.fromEntries(
Expand All @@ -510,10 +476,8 @@ const ObjectGenerator = ({ schema } = {}) =>
* The every() method tests whether all elements in the object pass the
* test implemented by the provided function.
* It returns a Boolean value.
*
* @param {Function} callbackFunction
*
* @return {boolean}
* @returns {boolean}
*/
flatEvery(callbackFunction) {
return this.values().every((value) => callbackFunction(value));
Expand All @@ -525,10 +489,8 @@ const ObjectGenerator = ({ schema } = {}) =>
* It returns true if, in the object, it finds an element for which the
* provided function returns true; otherwise it returns false.
* It doesn't modify the object.
*
* @param {Function} callbackFunction
*
* @return {boolean}
* @returns {boolean}
*/
flatSome(callbackFunction) {
return this.values().some((value) => callbackFunction(value));
Expand Down Expand Up @@ -569,22 +531,18 @@ const ObjectGenerator = ({ schema } = {}) =>

/**
* Create for every item in the array an anvancd object.
*
* @param {Array} data
*
* @return {Array}
* @returns {Array}
*/
static createAll(data) {
return data.map((item) => Obj.create(item));
}

/**
* Parse the data, so it converts all values to the given schema.
*
* @param {object} data
* @param {object} options
*
* @return {object}
* @returns {object}
*/
static parse(data, options = {}) {
const ParseOptions = ObjectGenerator({
Expand All @@ -603,10 +561,8 @@ const ObjectGenerator = ({ schema } = {}) =>

/**
* Parse every item in the array.
*
* @param {Array} data
*
* @return {Array}
* @returns {Array}
*/
static parseAll(data) {
return data.map((item) => Obj.parse(item));
Expand Down
Loading

0 comments on commit 3690af9

Please sign in to comment.