Security News
PyPI Introduces Digital Attestations to Strengthen Python Package Security
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
npm install dataway
yarn add dataway
Dataway
is an Algebra meant to represent any fetched data,
as such it streamline interaction with a fetched data in any of the following state, NotAsked
, Loading
, Failure
, Success
.
This allow you to interact safely with all those state without having to write multiple conditionnal branch tourought your code.
Imagine that our application rely on a webservice that provide us with a list of elements, and that our job is to both store the number of element in the application state for future usage, and display it.
import { dataway } from 'dataway';
// setup you initial state
let asyncValue = dataway.notAsked();
function loadBlogPosts() {
asyncValue = dataway.loading();
fetch('/blogposts')
.then(function(response) {
if (response.ok) {
try {
return response.json();
} catch (error) {
throw new Error('Response format was not ok.');
}
asyncValue = dataway.success(response);
} else {
throw new Error('Network response was not ok.');
}
})
.then(function(values) {
asyncValue = dataway.success(values);
})
.catch(function(error) {
asyncvalue = dataway.failure(error);
});
}
asyncvalue
// when working with the value you can safely ignore the real status of the value
.map(function(blogposts) {
return blogposts.length;
})
// when releasing the value you must handle all four cases
.fold(
'Number of blog posts not loaded',
'Number of blog posts loading',
function(error) {
`Number of blog posts can't be shown because : ${error}`;
},
function(value) {
return `There is ${value} blogposts`;
},
);
The different algebras used can be used in the following fashion: Using the instance method
const stringLength = string => string.length;
success('abc').map(stringLength);
// => Success(3)
The provided map implementation
dataway.map(success('abc'), stringLength);
// => Success(3)
Or any fantasyland complient library
import map from 'ramda/map';
map(success('abc'), stringLength);
// => Success(3)
At some point you will want to pull the trigger and use the values tucked inside, but since we want you to have a safe code, you will have to provide handler for each case.
The following example could be used with react.
dataway
.map(success('abc'), stringLength)
.fold(
() => <NoData />,
() => <Pending />,
error => <Failure error={error} />,
data => <span>{data}</span>,
);
The functor interface allow you to map over the Success
value of Dataway
.
const stringLength = string => string.length;
dataway.map(success('abc'), stringLength);
// => Success(3)
dataway.map(failure('xyz'), stringLength);
// => Failure('xyz')
dataway.map(loading(), stringLength);
// => Loading()
dataway.map(notAsked(), stringLength);
// => NotAsked()
The apply interface allow you to safely apply Dataway value to another one
const f = (s1: string, s2: string) => `${s1}${s2}`;
success('abc')
.map(f)
.ap(success('def'));
// => success('abcdef')
success('abc')
.map(f)
.ap(failure('xyz'));
// => failure('xyz');
success('abc')
.map(f)
.ap(loading());
// => loading();
success('abc')
.map(f)
.ap(notAsked());
// => notAsked();
You can note a pattern here that will apply to many function down this documentation.
when composing with multiple Dataway
the following rules apply in order:
success
, the result is a success
.failure
, the result is a failure
.failure
will be used to generate the end result failure
.loading
, the result is a loading
.notAsked
, the result is notAsked
.Apply
is a very interesting tool from wich you can implement many usefull function.
Append
const append = (dataway1, dataway2) =>
dataway1.map(value1 => value2 => [value1, value2]).ap(dataway2);
append(success('abc'), success('def'));
// => success(['abc', 'def'])
append(success('abc'), failure('xyz'));
// => failure('xyz')
Map2
const f = s1 => s2 => `${s1}${s2}`;
const map2 = (f, dataway1, dataway2) => ap(dataway1.map(f), dataway2);
map2(f, success('abc'), success('def'));
// => success('abcdef')
map2(f, success('abc'), failure('xyz'));
// => failure('xyz')
The chain function is usefull for applying one or many computation that may fail.
A good example of that is validating the content of Dataway
const data = success([{}, {}, {}]);
data
.chain(value =>
Array.isArray(value) ? success(value) : failure('should be an array'),
)
.chain(value =>
value[0] !== undefined ? success(value) : failure('should not be empty'),
);
// => success([{}, {}, {}])
FAQs
## Installation
The npm package dataway receives a total of 6 weekly downloads. As such, dataway popularity was classified as not popular.
We found that dataway demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.