Skip to content

Commit

Permalink
merged fn and fnjson
Browse files Browse the repository at this point in the history
  • Loading branch information
abumq committed Jul 25, 2023
1 parent b9d56ef commit 64c9a05
Show file tree
Hide file tree
Showing 10 changed files with 15 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
with:
node-version: 16
- run: yarn
- run: yarn test
- run: yarn test:ci

publish-npm:
needs: build
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ jobs:
with:
node-version: ${{ matrix.node-version }}
- run: yarn
- run: yarn test
- run: yarn test:ci
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## Unreleased
- Control collision and race-condition for spread keys
- Merged `fnjson` and `fn` functionalities

## 1.0.8
- Fix: Typed arrays are no longer going to return `Array`
Expand Down
7 changes: 0 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,6 @@ const result = await getDetail(getAge());

Try it on [RunKit](https://npm.runkit.com/airsync)

## Function with JSON
`fn()` is good when the function takes normal parameters, e.g, `myfn(param1, param2)` but this will not work if we have json based parameters, e.g, `myfn({ param1: 1, param2: Promise.resolve(2) })`

For this `fnjson()` was added.

Simply pass in function in to this function.

```javascript
const getName = ({ firstName, lastName }) => `${firstName} ${lastName}`;

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"rimraf": "^3.0.2"
},
"scripts": {
"test:ci": "TEST_CI=true mocha tests",
"test": "mocha tests",
"tdd": "mocha tests --watch",
"build": "rimraf dist && mkdir dist && minify src -d dist",
Expand Down
2 changes: 1 addition & 1 deletion src/fn.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ const fn = (func, opt = {}) => {
}
const finalFunc = (...args) => exec(opt, func, ...args);
finalFunc._isAirsync = true;
finalFunc._airsyncOptions = opt;
finalFunc._airsyncOptions = opt || {};
finalFunc.setOptions = ({ startTime, endTime, debug, name, description }) => {
// we use destructured option instead of Object.keys in params for performance
finalFunc._airsyncOptions.endTime = endTime || finalFunc._airsyncOptions.endTime;
Expand Down
5 changes: 3 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ const { json, spread } = require('./json');
module.exports = fn;

// name exports from fn
module.exports.fn = fn;
module.exports.fnExport = fnExport;
module.exports.exec = exec;
module.exports.fnjson = (theSyncFunc) => fn(async (obj, ...anythingElse) => theSyncFunc(await json(obj), ...anythingElse));
module.exports.fnjson = (theSyncFunc, opts = {}) => fn(async (obj, ...anythingElse) => theSyncFunc(await json(obj), ...anythingElse), opts);

module.exports.fn = module.exports.fnjson;

// name exports from json
module.exports.json = json;
Expand Down
1 change: 0 additions & 1 deletion src/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ const createObject = (obj, depth, currentKey, opts = {}) => {
const finalValue = await createObject(values[keyIdx], 0, key);

if (key.indexOf(SPREAD_KEY_NAME) === 0) {
console.log(key)
Object.assign(finalResult, finalValue);
} else {
Object.assign(finalResult, { [key] : finalValue });
Expand Down
10 changes: 7 additions & 3 deletions tests/spread.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ describe('When we have JSON with spread()', async () => {
}

const props = await buildProps();
// console.log(props)

it('the JSON correctly spreads the properties', () => {
assert.equal(props.name, 'John');
Expand Down Expand Up @@ -61,8 +60,13 @@ describe('When we have JSON with spread()', async () => {
});

describe('Test collision for spread key', () => {
const TEST_COUNT = 10000;
const LOG_FREQ = 1000;
let TEST_COUNT = 10000;

if (process.env.TEST_CI) {
TEST_COUNT = 50;
}
const LOG_FREQ = TEST_COUNT / 10;

const list = [];

it(`Ensure no spread key is same when produced ${TEST_COUNT} times`, () => {
Expand Down
2 changes: 0 additions & 2 deletions tests/typed-arrays.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
const assert = require('assert');
const airsync = require('../src');

const p = async v => v;

describe('Typed arrays are resolved correctly', () => {

describe('Uint8Array', async () => {
Expand Down

0 comments on commit 64c9a05

Please sign in to comment.