instant-utils
Advanced tools
Comparing version 0.0.3 to 1.0.0
@@ -29,3 +29,3 @@ 'use strict'; | ||
var asyncForEach = exports.asyncForEach = function () { | ||
var _ref = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee(array, callback) { | ||
var _ref = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee(arr, iteratee) { | ||
var index; | ||
@@ -39,3 +39,3 @@ return _regenerator2.default.wrap(function _callee$(_context) { | ||
case 1: | ||
if (!(index < array.length)) { | ||
if (!(index < arr.length)) { | ||
_context.next = 7; | ||
@@ -46,3 +46,3 @@ break; | ||
_context.next = 4; | ||
return callback(array[index], index, array); | ||
return iteratee(arr[index], index, arr); | ||
@@ -68,3 +68,3 @@ case 4: | ||
var asyncMap = exports.asyncMap = function () { | ||
var _ref2 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee2(array, callback) { | ||
var _ref2 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee2(arr, iteratee) { | ||
var results, index, result; | ||
@@ -79,3 +79,3 @@ return _regenerator2.default.wrap(function _callee2$(_context2) { | ||
case 2: | ||
if (!(index < array.length)) { | ||
if (!(index < arr.length)) { | ||
_context2.next = 10; | ||
@@ -86,3 +86,3 @@ break; | ||
_context2.next = 5; | ||
return callback(array[index], index, array); | ||
return iteratee(arr[index], index, arr); | ||
@@ -89,0 +89,0 @@ case 5: |
{ | ||
"name": "instant-utils", | ||
"version": "0.0.3", | ||
"description": "Utility methods", | ||
"version": "1.0.0", | ||
"description": "Lightweight utility library to assist other `instant` packages.", | ||
"main": "lib/index.js", | ||
@@ -14,3 +14,3 @@ "scripts": { | ||
"type": "git", | ||
"url": "git+https://github.com/JSJInvestments/instant-utils.git" | ||
"url": "git+https://github.com/cjmyles/instant-utils.git" | ||
}, | ||
@@ -20,3 +20,3 @@ "keywords": [ | ||
], | ||
"author": "Craig Myles (https://instantfeedback.com.au)", | ||
"author": "Craig Myles", | ||
"license": "MIT", | ||
@@ -23,0 +23,0 @@ "dependencies": {}, |
138
README.md
# Instant Utils | ||
Javascript helper methods. | ||
Lightweight utility library to assist other `instant` packages. | ||
_**Please note:** This is specifically tailored for `instant` packages. If you are not a member of the Instant Feedback team, please avoid using this package_ | ||
## Table of Contents | ||
## Features | ||
- [Installation](#installation) | ||
- [Usage](#usage) | ||
- [API](#api) | ||
- [Async For Each](#async-for-each) | ||
- [Async Map](#async-map) | ||
- [Pick](#pick) | ||
- [Remove Undefineds](#removeUndefineds) | ||
- [Contributing](#contributing) | ||
- [License](#license) | ||
- Async For Each | ||
- Async Map | ||
## Installation | ||
You can install this wrapper by running the following in your project: | ||
You can install this package using npm: | ||
@@ -22,8 +27,12 @@ ```bash | ||
Here is a quick example to get you started: | ||
**ES Modules** | ||
```javascript | ||
import { asyncMap } from 'instant-utils'; | ||
import { pick } from 'instant-utils'; | ||
// Do stuff with asyncMap or any of the other Utils you import | ||
const country = { id: 1, name: 'Australia' }; | ||
const result = pick(country, ['name']); | ||
console.log(result); // => { name: 'Australia' } | ||
``` | ||
@@ -34,19 +43,114 @@ | ||
```javascript | ||
const utils = require('instant-utils'); | ||
var utils = require('instant-utils'); | ||
// Do stuff with asyncMap or any of the other Utils you import | ||
var country = { id: 1, name: 'Australia' }; | ||
var result = utils.pick(country, ['name']); | ||
console.log(result); // => { name: 'Australia' } | ||
``` | ||
## Running Tests | ||
## API | ||
To run the tests, clone the repository and install the dependencies: | ||
### Async For Each | ||
```bash | ||
git clone https://github.com/JSJInvestments/instant-utils.git | ||
cd instant-utils && npm i | ||
npm run test | ||
Asyncronous For Each function, enabling multiple loop iterations to be wrapped in one completion promise. | ||
#### Arguments | ||
`arr (Array)`: The array to iterate over.\ | ||
`iteratee (Function)`: The function invoked per iteration. | ||
#### Returns | ||
`Array` | ||
#### Example Usage | ||
```js | ||
async function getCountries() { | ||
const countries = ['scotland', 'australia'], | ||
]; | ||
return await asyncForEach(countries, country => { | ||
const response = await fetch(`/api/countries/${country}`); | ||
return await response.json(); | ||
}); | ||
} | ||
``` | ||
### Async Map | ||
Asyncronous Map function, enabling multiple map iterations to be wrapped in one completion promise. | ||
#### Arguments | ||
`arr (Array)`: The array to iterate over.\ | ||
`iteratee (Function)`: The function invoked per iteration. | ||
#### Returns | ||
`Array` | ||
#### Example Usage | ||
```js | ||
async function mapCountries() { | ||
const countries = [ | ||
{ id: 'scotland', name: 'Scotland' }, | ||
{ id: 'australia', name: 'Australia' }, | ||
]; | ||
return await asyncMap(countries, country => { | ||
return { | ||
...country, | ||
visited: true, | ||
}; | ||
}); | ||
} | ||
``` | ||
### Pick | ||
Creates an object composed of the picked object properties. | ||
#### Arguments | ||
`obj (Object)`: The source object.\ | ||
`keys (Array)`: The property keys to pick. | ||
#### Returns | ||
`Object` | ||
#### Example Usage | ||
```js | ||
const country = { id: 1, name: 'Australia' }; | ||
const result = pick(country, ['name']); | ||
console.log(result); // => { name: 'Australia' } | ||
``` | ||
### Remove Undefineds | ||
Creates an object composed of non-undefined-valued object properties. | ||
#### Arguments | ||
`obj (Object)`: The source object.\ | ||
#### Returns | ||
`Object` | ||
#### Example Usage | ||
```js | ||
const country = { id: 1, name: 'Australia', unicorns: undefined }; | ||
const result = removeUndefineds(country); | ||
console.log(result); // => { id: 1, name: 'Australia' } | ||
``` | ||
## Contributing | ||
We'd greatly appreciate any [contribution](CONTRIBUTING.md) you make. | ||
## License | ||
[MIT](LICENSE) |
@@ -1,11 +0,11 @@ | ||
export async function asyncForEach(array, callback) { | ||
for (let index = 0; index < array.length; index++) { | ||
await callback(array[index], index, array); | ||
export async function asyncForEach(arr, iteratee) { | ||
for (let index = 0; index < arr.length; index++) { | ||
await iteratee(arr[index], index, arr); | ||
} | ||
} | ||
export async function asyncMap(array, callback) { | ||
export async function asyncMap(arr, iteratee) { | ||
let results = []; | ||
for (let index = 0; index < array.length; index++) { | ||
const result = await callback(array[index], index, array); | ||
for (let index = 0; index < arr.length; index++) { | ||
const result = await iteratee(arr[index], index, arr); | ||
results.push(result); | ||
@@ -12,0 +12,0 @@ } |
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
13521
12
1
155