New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

another-maybe

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

another-maybe - npm Package Compare versions

Comparing version 0.0.2 to 0.0.3

src/test.js

4

package.json
{
"name": "another-maybe",
"version": "0.0.2",
"version": "0.0.3",
"description": "Another maybe library which thinks it got it all.",

@@ -16,5 +16,5 @@ "main": "src/index.js",

"mocha": "^3.2.0",
"mocha-prepare-promise": "^0.0.1",
"mocha-prepare-promise": "^0.0.2",
"referee": "^1.2.0"
}
}

@@ -5,2 +5,4 @@ # maybe

With version **0.0.3** there is full support of promises.
## Where to use

@@ -11,3 +13,3 @@

## Installation
```
```sh
npm install another-maybe --save-dev

@@ -19,3 +21,3 @@ ```

#### Classic default value handling
```
```javascript
const value = maybe('originalValue')

@@ -27,3 +29,3 @@ .orValue('alternativeValue')

#### Classic default value handling via callback
```
```javascript
const value = maybe('originalValue')

@@ -35,3 +37,3 @@ .orElse(() => 'alternativeValue')

#### Usage of is method
```
```javascript
const value = maybe('originalValue')

@@ -44,3 +46,3 @@ .is((v) => v === 'originalValue')

#### Usage of map method
```
```javascript
const value = maybe('originalValue')

@@ -52,3 +54,3 @@ .map((v) => v + '1')

#### Usage of for-each method
```
```javascript
const value = maybe('originalValue')

@@ -60,3 +62,3 @@ .forEach((v) => console.log(v))

#### Usage in async operation
```
```javascript
const test = async () => delayFunction(200);

@@ -76,1 +78,41 @@ const value = await maybe('originalValue')

```
#### Usage in an advanced async operation
```javascript
const test = async () => delayFunction(200);
const value = await maybe(1)
.map(async (v) => {
await test();
return v + 1;
}) // returns Promise with value "2"
.is(async (v) => {
return v === 1;
}) // returns Promise with value undefined
.orElse(async () => {
await test();
return 0;
}) // returns Promise with value 0
.forEach((v) => {
console.log(v);
})
.map(async (v) => {
await test();
return undefined;
}) // returns Promise with value undefined
.forEach((v) => {
console.log(v);
})
.orValue(2) // returns Promise with value 2
.map(async (v) => {
await test();
return v + 1;
}) // returns Promise with value 3
.get();
/* end result: value === 3 */
```
'use strict';
exports.isPromise = (value) => {
return typeof value === 'object' && value instanceof Promise;
};
exports.isNothing = (value) => {
return value === undefined || value === null;
};
exports.nil = () => {
return void 0;
};

@@ -6,9 +6,26 @@ 'use strict';

function maybe(value, handlers) {
const {isPromise} = handlers;
return isPromise(value)
? asyncMaybe(value, handlers)
: syncMaybe(value, handlers);
}
function syncMaybe(value, handlers) {
const {isPromise, nil} = handlers;
const morph = (v) => maybe(v, handlers);
const isNothing = () => handlers.isNothing(value);
const maybeInterface = {
isNothing: isNothing,
is: (fn) => !isNothing() && fn(value) ? maybeInterface : morph(),
isNothing,
is: (fn) => {
const conditional = !isNothing() && fn(value);
if (isPromise(conditional)) {
return morph(conditional.then((isValid) => isValid ? value : nil()));
}
return conditional ? maybeInterface : morph();
},
map: (fn) => isNothing() ? maybeInterface : morph(fn(value)),
flatMap: (fn) => isNothing() ? null : fn(value),
flatMap: (fn) => isNothing() ? nil() : fn(value),
forEach: (fn) => {

@@ -20,3 +37,3 @@ if (!isNothing()) fn(value);

orValue: (v) => isNothing() ? morph(v) : maybeInterface,
get: () => value
get: () => isNothing() ? nil() : value
};

@@ -27,2 +44,18 @@

function asyncMaybe(value, handlers) {
const morph = (v) => asyncMaybe(v, handlers);
const maybeInterface = syncMaybe(value, handlers);
const flatResolve = (arg, key) => (v) => maybe(v, handlers)[key](arg);
const resolve = (arg, key) => (v) => flatResolve(arg, key)(v).get();
return Object.assign({}, maybeInterface, {
is: (fn) => morph(value.then(resolve(fn, 'is'))),
map: (fn) => morph(value.then(resolve(fn, 'map'))),
flatMap: (fn) => value.then(flatResolve(fn, 'flatMap')),
forEach: (fn) => morph(value.then(resolve(fn, 'forEach'))),
orElse: (fn) => morph(value.then(resolve(fn, 'orElse'))),
orValue: (v) => morph(value.then(resolve(v, 'orValue')))
});
}
function provider(handlers) {

@@ -29,0 +62,0 @@ return (value) => {

@@ -14,3 +14,3 @@ 'use strict';

assert.equals(value1, 'test');
assert.equals(value2, null);
assert.equals(value2, undefined);
assert.equals(value3, undefined);

@@ -98,7 +98,7 @@ });

assert.equals(value1, 'test1');
assert.equals(value2, null);
assert.equals(value2, undefined);
});
it('doing some async operation', async function () {
const test = async () => delay(200);
it('doing basic async operations', async function () {
const test = async () => delay(10);
const result1 = await maybe('test')

@@ -132,2 +132,44 @@ .map(async (v) => {

});
it('doing advanced async operations', async function () {
const test = async () => delay(10);
let runFirstForEach = false;
let runSecondForEach = false;
const result1 = await maybe(1)
.map(async (v) => {
await test();
return v + 1;
})
.is(async (v) => {
return v === 1;
})
.orElse(async () => {
await test();
return 0;
})
.forEach((v) => {
runFirstForEach = true;
})
.map(async (v) => {
await test();
return undefined;
})
.forEach((v) => {
runSecondForEach = true;
})
.orValue(2)
.map(async (v) => {
await test();
return v + 1;
})
.get();
assert.equals(result1, 3);
assert.equals(runFirstForEach, true);
assert.equals(runSecondForEach, false);
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc