Skip to content

Commit

Permalink
feat(transformer): add sync transform block
Browse files Browse the repository at this point in the history
  • Loading branch information
lykmapipo committed Mar 21, 2021
1 parent 3933661 commit c50639e
Show file tree
Hide file tree
Showing 7 changed files with 7,110 additions and 7,556 deletions.
1 change: 1 addition & 0 deletions .husky/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
_
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npm test
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#### 0.42.11 (2021-03-21)

#### 0.42.10 (2021-02-20)

#### 0.42.9 (2021-02-08)

##### Chores
Expand Down
14,546 changes: 7,013 additions & 7,533 deletions package-lock.json

Large diffs are not rendered by default.

48 changes: 25 additions & 23 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"name": "@lykmapipo/common",
"version": "0.42.9",
"version": "0.42.11",
"description": "Helper utilities for day to day development",
"main": "lib/index.js",
"module": "es/index.js",
"scripts": {
"clean": "rimraf lib dist es umd logs",
"prepare": "husky install",
"prebuild": "npm test && npm run clean",
"build": "rollup -c",
"lint": "eslint --fix --ext .js src/ test/ rollup.config.js",
Expand All @@ -17,7 +18,8 @@
"docs": "doxdox 'lib/**/*.js' -p package.json -l markdown -o DOCUMENTATION.md",
"cmt": "git add -A && git-cz",
"changelog": "changelog",
"chore:deps": "ncu -u && npm install --save"
"chore:deps": "ncu -u && npm install --save",
"prepare": "husky install"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -48,41 +50,41 @@
},
"homepage": "https://github.com/lykmapipo/common",
"devDependencies": {
"@babel/cli": "^7.12.13",
"@babel/core": "^7.12.13",
"@babel/node": "^7.12.13",
"@babel/preset-env": "^7.12.13",
"@babel/register": "^7.12.13",
"@commitlint/cli": "^11.0.0",
"@commitlint/config-conventional": "^11.0.0",
"@commitlint/travis-cli": "^11.0.0",
"@lykmapipo/test-helpers": "^0.6.11",
"acorn": "^8.0.5",
"chai": "^4.3.0",
"@babel/cli": "^7.13.10",
"@babel/core": "^7.13.10",
"@babel/node": "^7.13.10",
"@babel/preset-env": "^7.13.10",
"@babel/register": "^7.13.8",
"@commitlint/cli": "^12.0.1",
"@commitlint/config-conventional": "^12.0.1",
"@commitlint/travis-cli": "^12.0.1",
"@lykmapipo/test-helpers": "^0.6.13",
"acorn": "^8.1.0",
"chai": "^4.3.4",
"commitizen": "^4.2.3",
"coveralls": "^3.1.0",
"cz-conventional-changelog": "^3.3.0",
"dot-prop": "^6.0.1",
"doxdox": "^3.0.0",
"eslint": "^7.19.0",
"eslint": "^7.22.0",
"eslint-config-airbnb-base": "^14.2.1",
"eslint-config-prettier": "^7.2.0",
"eslint-config-prettier": "^8.1.0",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-jsdoc": "^31.6.1",
"eslint-plugin-mocha": "^8.0.0",
"eslint-plugin-jsdoc": "^32.3.0",
"eslint-plugin-mocha": "^8.1.0",
"eslint-plugin-prettier": "^3.3.1",
"generate-changelog": "^1.8.0",
"handlebars": "^4.7.6",
"husky": "^4.3.8",
"handlebars": "^4.7.7",
"husky": "^5.1.3",
"kind-of": "^6.0.3",
"lint-staged": "^10.5.4",
"lodash.template": "^4.5.0",
"minimist": "^1.2.5",
"mocha": "^8.2.1",
"mocha": "^8.3.2",
"nyc": "^15.1.0",
"prettier": "^2.2.1",
"rimraf": "^3.0.2",
"rollup": "^2.38.5",
"rollup": "^2.42.1",
"shelljs": "^0.8.4",
"websocket-extensions": "^0.1.4"
},
Expand All @@ -91,8 +93,8 @@
"browser-or-node": ">=1.3.0",
"flat": ">=5.0.2",
"inflection": ">=1.12.0",
"lodash": ">=4.17.20",
"mime": ">=2.5.0",
"lodash": ">=4.17.21",
"mime": ">=2.5.2",
"moment": ">=2.29.1",
"object-hash": ">=2.1.1",
"parse-json": ">=5.2.0",
Expand Down
52 changes: 52 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import {
omitBy,
orderBy,
reduce,
size,
some,
snakeCase,
startCase,
Expand Down Expand Up @@ -1479,6 +1480,57 @@ export const join = (values = [], separator = ', ', property = '') => {
return joined;
};

/**
* @function transform
* @name transform
* @description Preprocess given values according to provided transformers
* @param {*} vals value to be convert
* @param {Function} [transformers] iteratee function which receive result
* `value` to be transformed
* @returns {*} resulted value
* @author lally elias <lallyelias87@gmail.com>
* @license MIT
* @since 0.43.0
* @version 0.1.0
* @static
* @public
* @example
*
* const transform = transform(['a']);
* // => ['a']
*
* const transform = transform([1, '2'], _.toNumber);
* // => [1, 2]
*
*/
export const transform = (vals, ...transformers) => {
// ensure compact values
const values = compact([].concat(vals));

// prepare transformers
const defaultTransformer = (value) => value;
const preprocessors = map(
compact([defaultTransformer].concat(...transformers)),
(transformer) => {
return isFunction(transformer) ? transformer : defaultTransformer;
}
);

// transform values
let transformed = map(values, (value) => {
let data;
forEach(preprocessors, (transformer) => {
data = transformer(value);
});
return data;
});

// return transformed data
transformed = compact(transformed);
transformed = size(transformed) === 1 ? first(transformed) : transformed;
return transformed;
};

/**
* @function arrayToObject
* @name arrayToObject
Expand Down
11 changes: 11 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import {
flat,
unflat,
join,
transform,
arrayToObject,
parseMs,
wrapCallback,
Expand Down Expand Up @@ -777,6 +778,16 @@ describe('common', () => {
});
});

it('should transform values', () => {
const asNumber = (val) => Number(val);

expect(transform('1')).to.be.eql('1');
expect(transform('1', asNumber)).to.be.eql(1);

expect(transform(['1', '2'])).to.be.eql(['1', '2']);
expect(transform(['1', '2'], asNumber)).to.be.eql([1, 2]);
});

it('should parse milliseconds', () => {
expect(parseMs(1337000001)).to.be.eql({
days: 15,
Expand Down

0 comments on commit c50639e

Please sign in to comment.