@trojs/mutator 0.4.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 TroJS
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,75 @@
1
+ # Mutate the value when set some data
2
+
3
+ [![NPM version][npm-image]][npm-url] [![Coveralls Status][coveralls-image]][coveralls-url]
4
+
5
+ With this package you can define some setters that are optional.
6
+ So it change the value for the defined setters, but if you don't pass the key, it doesnt set the key.
7
+
8
+ ## Installation
9
+
10
+ `npm install @trojs/objects`
11
+ or
12
+ `yarn add @trojs/objects`
13
+
14
+ ## Test the package
15
+
16
+ `npm run test`
17
+ or
18
+ `yarn test`
19
+
20
+ ## Example usage
21
+
22
+ ```javascript
23
+ import DefaultMutator from '@trojs/mutator'
24
+
25
+ class ExampleMutator extends DefaultMutator {
26
+ setSkuAttribute (sku) {
27
+ return `*${sku}*`
28
+ }
29
+ }
30
+
31
+ const result = ExampleMutator.create({ sku: '42', test: 'ok' })
32
+ {
33
+ sku: '*42*',
34
+ test: 'ok'
35
+ }
36
+
37
+ const result = ExampleMutator.create({ sku: '42' })
38
+ {
39
+ sku: '*42*'
40
+ }
41
+
42
+ const result = ExampleMutator.create({ test: 'ok' })
43
+ {
44
+ test: 'ok'
45
+ }
46
+ ```
47
+
48
+ You can also hydrate the object with new data
49
+
50
+ ```javascript
51
+ const result = ExampleMutator.create({ test: 'ok', test2: 'also ok' })
52
+ {
53
+ test: 'ok',
54
+ test2: 'also ok'
55
+ }
56
+
57
+ result.hydrate({ sku: 43})
58
+ {
59
+ test: 'ok',
60
+ test2: 'also ok',
61
+ sku: '*43*'
62
+ }
63
+
64
+ result.hydrate({ test: 'another text'})
65
+ {
66
+ test: 'another text',
67
+ test2: 'also ok',
68
+ sku: '*43*'
69
+ }
70
+ ```
71
+
72
+ [npm-url]: https://www.npmjs.com/package/@trojs/mutator
73
+ [npm-image]: https://img.shields.io/npm/v/@trojs/mutator.svg
74
+ [coveralls-url]: https://coveralls.io/r/hckrnews/mutator
75
+ [coveralls-image]: https://img.shields.io/coveralls/hckrnews/mutator/main.svg
package/package.json ADDED
@@ -0,0 +1,60 @@
1
+ {
2
+ "name": "@trojs/mutator",
3
+ "description": "Mutate the value when set some data",
4
+ "version": "0.4.0",
5
+ "author": {
6
+ "name": "Pieter Wigboldus",
7
+ "url": "https://trojs.org/"
8
+ },
9
+ "license": "MIT",
10
+ "scripts": {
11
+ "lint": "eslint src/*.js --config .eslintrc",
12
+ "lint:fix": "eslint src/*.js --config .eslintrc --fix",
13
+ "lint:report": "eslint src/*.js --config .eslintrc -f json -o report.json",
14
+ "test": "jest",
15
+ "test:watch": "jest src --watch",
16
+ "coveralls": "jest && codecov && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js",
17
+ "cpd": "node_modules/jscpd/bin/jscpd src",
18
+ "vulnerabilities": "npm audit --only=prod"
19
+ },
20
+ "type": "module",
21
+ "files": [
22
+ "src/default.js",
23
+ "src/string-helper.js"
24
+ ],
25
+ "main": "src/default.js",
26
+ "devDependencies": {
27
+ "@babel/core": "^7.9.0",
28
+ "@babel/eslint-parser": "^7.14.7",
29
+ "@babel/plugin-transform-modules-commonjs": "^7.14.0",
30
+ "@babel/preset-env": "^7.9.5",
31
+ "@jest/globals": "^29.0.0",
32
+ "babel-jest": "^29.0.0",
33
+ "eslint": "^8.6.0",
34
+ "eslint-config-airbnb-base": "^15.0.0",
35
+ "eslint-config-prettier": "^9.0.0",
36
+ "eslint-config-standard": "^17.1.0",
37
+ "eslint-plugin-html": "^8.0.0",
38
+ "eslint-plugin-import": "^2.28.1",
39
+ "eslint-plugin-jsdoc": "^48.0.4",
40
+ "eslint-plugin-jsx-a11y": "^6.4.1",
41
+ "eslint-plugin-n": "^16.2.0",
42
+ "eslint-plugin-prettier": "^5.0.0",
43
+ "eslint-plugin-promise": "^6.1.1",
44
+ "eslint-plugin-sonarjs": "^0.24.0",
45
+ "jest": "^29.0.0",
46
+ "jscpd": "^3.2.1",
47
+ "prettier": "^3.0.0"
48
+ },
49
+ "repository": {
50
+ "type": "git",
51
+ "url": "https://github.com/hckrnews/mutator"
52
+ },
53
+ "engines": {
54
+ "node": ">= 18"
55
+ },
56
+ "keywords": [
57
+ "object",
58
+ "mutate"
59
+ ]
60
+ }
package/src/default.js ADDED
@@ -0,0 +1,45 @@
1
+ import { capitalizeWords } from './string-helper.js'
2
+
3
+ export default class DefaultMutator {
4
+ mapper ([key, value]) {
5
+ if (value === undefined || value === null || Number.isNaN(value)) {
6
+ return [key, undefined]
7
+ }
8
+
9
+ if (value.constructor === Object) {
10
+ const mappedResult = Object.entries(value).map(
11
+ ([subKey, subValue]) => {
12
+ const result = [`${key}_${subKey}`, subValue]
13
+ return [subKey, this.mapper(result)[1]]
14
+ }
15
+ )
16
+
17
+ const filteredResult = mappedResult.filter(Boolean)
18
+
19
+ return [key, Object.fromEntries(filteredResult)]
20
+ }
21
+
22
+ const fn = `set${capitalizeWords(key)}Attribute`
23
+ if (this?.[fn]?.constructor === Function) {
24
+ return [key, this[fn](value)]
25
+ }
26
+
27
+ return [key, value]
28
+ }
29
+
30
+ hydrate (data) {
31
+ const result = Object.entries(data).map(this.mapper.bind(this))
32
+ const filteredResult = result.filter(
33
+ ([key, value]) => key && value !== undefined
34
+ )
35
+
36
+ Object.assign(this, Object.fromEntries(filteredResult))
37
+ }
38
+
39
+ static create (data) {
40
+ const view = new this()
41
+ view.hydrate(data)
42
+
43
+ return view
44
+ }
45
+ }
@@ -0,0 +1,8 @@
1
+ export const capitalizeFirstLetter = (string) =>
2
+ string.charAt(0).toUpperCase() + string.slice(1)
3
+
4
+ export const capitalizeWords = (string) =>
5
+ string
6
+ .split(/_| /)
7
+ .map((word) => capitalizeFirstLetter(word))
8
+ .join('')