Permalink
Browse files

(re) Consistent coding/spelling/API styles

  • Loading branch information...
robotlolita committed Mar 5, 2017
1 parent abdf600 commit f0dd12069877ecdfb9f3f8bd0680a6ad668190a5
Showing with 235 additions and 990 deletions.
  1. +10 −5 package.json
  2. +25 −131 src/core/adt/data.js
  3. +2 −30 src/core/adt/{show.js → derivations/debug-representation.js}
  4. +4 −16 src/core/adt/{setoid.js → derivations/equality.js}
  5. +17 −0 src/core/adt/derivations/index.js
  6. +2 −54 src/core/adt/{serialize.js → derivations/serialization.js}
  7. +2 −6 src/core/adt/index.js
  8. +0 −6 src/core/fantasy-land/apply.js
  9. +0 −6 src/core/fantasy-land/bimap.js
  10. +0 −7 src/core/fantasy-land/chain.js
  11. +0 −6 src/core/fantasy-land/concat.js
  12. +0 −3 src/core/fantasy-land/curried.js
  13. +0 −6 src/core/fantasy-land/empty.js
  14. +0 −6 src/core/fantasy-land/equals.js
  15. +0 −3 src/core/fantasy-land/index.js
  16. +0 −3 src/core/fantasy-land/infix.js
  17. +0 −6 src/core/fantasy-land/map.js
  18. +0 −6 src/core/fantasy-land/of.js
  19. +1 −3 src/core/index.js
  20. +3 −9 src/core/lambda/compose.js
  21. +1 −4 src/core/lambda/constant.js
  22. +1 −4 src/core/lambda/curry.js
  23. +1 −4 src/core/lambda/identity.js
  24. +2 −4 src/core/lambda/index.js
  25. +2 −8 src/core/lambda/{partialise.js → partialize.js}
  26. +1 −4 src/core/object/from-pairs.js
  27. +1 −3 src/core/object/index.js
  28. +3 −9 src/core/object/map-entries.js
  29. +2 −6 src/core/object/map-values.js
  30. +0 −2 src/core/object/to-pairs.js
  31. +0 −2 src/core/object/values.js
  32. +1 −4 src/data/conversions/index.js
  33. +0 −2 src/data/conversions/maybe-to-result.js
  34. +0 −2 src/data/conversions/maybe-to-validation.js
  35. +0 −2 src/data/conversions/nullable-to-maybe.js
  36. +0 −2 src/data/conversions/nullable-to-result.js
  37. +0 −2 src/data/conversions/nullable-to-validation.js
  38. +0 −2 src/data/conversions/result-to-maybe.js
  39. +1 −3 src/data/conversions/result-to-validation.js
  40. +0 −2 src/data/conversions/validation-to-maybe.js
  41. +1 −3 src/data/conversions/validation-to-result.js
  42. +5 −88 src/data/future/_deferred.js
  43. +5 −15 src/data/future/_execution-state.js
  44. +0 −162 src/data/future/_future.js
  45. +1 −5 src/data/future/index.js
  46. +1 −4 src/data/index.js
  47. +1 −9 src/data/maybe/index.js
  48. +37 −82 src/data/maybe/maybe.js
  49. +1 −9 src/data/result/index.js
  50. +44 −72 src/data/result/result.js
  51. +0 −2 src/data/result/try.js
  52. +6 −0 src/data/task/_task-execution.js
  53. +15 −0 src/data/task/_task.js
  54. +2 −2 src/data/task/index.js
  55. +1 −0 src/data/task/task.js
  56. +1 −4 src/data/validation/index.js
  57. +29 −87 src/data/validation/validation.js
  58. +1 −1 src/helpers/extend.js
  59. +0 −48 src/helpers/provide-fantasy-land-aliases.js
  60. +2 −14 src/index.js
View
@@ -74,10 +74,15 @@
"metamagical-interface": "^3.4.0",
"metamagical-repl": "0.3.x"
},
"meta:magical": {
"copyright": "(c) 2015-2016 Quildreen Motta, and CONTRIBUTORS",
"maintainers": [
"Quildreen Motta <queen@robotlolita.me> (http://robotlolita.me/)"
]
"metamagical": {
"modulePath": {
"ignorePrefix": "src/"
},
"commonMetadata": {
"copyright": "(c) 2013-2017 Quildreen Motta, and CONTRIBUTORS",
"maintainers": [
"Quildreen Motta <queen@robotlolita.me> (http://robotlolita.me/)"
]
}
}
}
View
@@ -22,17 +22,17 @@ const keys = Object.keys;
// --[ Helpers ]--------------------------------------------------------
/*~
* Returns an array of own enumerable values in an object.
*/
//
// Returns an array of own enumerable values in an object.
//
function values(object) {
return keys(object).map(key => object[key]);
}
/*~
* Transforms own enumerable key/value pairs.
*/
//
// Transforms own enumerable key/value pairs.
//
function mapObject(object, transform) {
return keys(object).reduce((result, key) => {
result[key] = transform(key, object[key]);
@@ -43,9 +43,9 @@ function mapObject(object, transform) {
// --[ Variant implementation ]-----------------------------------------
/*~
* Defines the variants given a set of patterns and an ADT namespace.
*/
//
// Defines the variants given a set of patterns and an ADT namespace.
//
function defineVariants(typeId, patterns, adt) {
return mapObject(patterns, (name, constructor) => {
// ---[ Variant Internals ]-----------------------------------------
@@ -56,24 +56,16 @@ function defineVariants(typeId, patterns, adt) {
// This is internal, and we don't want the user to be messing with this.
[TAG]: name,
// This is documented by the user, so we don't re-document it.
constructor: constructor,
/*~~inheritsMeta: constructor */
get constructor() {
return constructor;
},
/*~
* True if a value belongs to the ADT variant.
*
* ---
* category: Testing and Comparing
* ~belongsTo: constructor
* deprecated:
* version: 2.0.0
* replacedBy: .hasInstance(value)
* reason: |
* Having a `value.isFoo` property doesn't allow people to
* differentiate two variants from different ADTs that have the
* same name. So, instead, now variants and ADTs come with a
* static `.hasInstance(value)` method.
*
* ~belongsTo: constructor
* replacedBy: .hasInstance(value)w
*/
get [`is${name}`]() {
warnDeprecation(`.is${name} is deprecated. Use ${name}.hasInstance(value)
@@ -82,36 +74,10 @@ instead to check if a value belongs to the ADT variant.`);
},
/*~
* Selects an operation based on this Variant's tag.
*
* The `matchWith` operation allows a very limited form of
* pattern matching, by selecting an operation depending on this
* value's tag.
*
*
* ## Example::
*
* const List = data('List', {
* Nil: () => ({}),
* Cons: (head, tail) => ({ head, tail })
* });
*
* const { Nil, Cons } = List;
*
* const sum = (list) => list.matchWith({
* Nil: () => 0,
* Cons: ({ head, tail }) => head + sum(tail)
* });
*
* sum(Cons(1, Cons(2, Nil()))); // ==> 3
*
* ---
* category : Transforming
* ~belongsTo: constructor
* type: |
* ('a is Variant).({ 'b: (Object Any) => 'c }) => 'c
* where 'b = 'a[`@@folktale:adt:tag]
*
* ~belongsTo: constructor
*/
matchWith(pattern) {
return pattern[name](this);
@@ -129,76 +95,28 @@ instead to check if a value belongs to the ADT variant.`);
// wrapper, which is what the user will interact with most of the time.
[META]: constructor[META],
/*~
* The unique tag for this variant within the ADT.
*
* ---
* category: State and Configuration
* ~belongsTo: makeInstance
*/
/*~~belongsTo: makeInstance */
get tag() {
return name;
},
/*~
* The (ideally unique) type for the ADT. This is provided by the user
* creating the ADT, so we can't actually guarantee uniqueness.
*
* ---
* category: State and Configuration
* ~belongsTo: makeInstance
*/
/*~~belongsTo: makeInstance */
get type() {
return typeId;
},
/*~
* The internal constructor provided by the user, which transforms and
* validates the properties attached to objects constructed in this ADT.
*
* ---
* category: Internal
* ~belongsTo: makeInstance
*/
/*~~belongsTo: makeInstance */
get constructor() {
return constructor;
},
/*~
* The object that provides common behaviours for instances of this variant.
*
* ---
* category: Internal
* ~belongsTo: makeInstance
*/
/*~~belongsTo: makeInstance */
prototype: InternalConstructor.prototype,
/*~
* Checks if a value belongs to this Variant.
*
* This is similar to the `ADT.hasInstance` check, with the
* exception that we also check if the value is of the same
* variant (has the same tag) as this variant.
*
*
* ## Example::
*
* const Either = data('Either', {
* Left: (value) => ({ value }),
* Right: (value) => ({ value })
* });
*
* const { Left, Right } = Either;
*
* Left.hasInstance(Left(1)); // ==> true
* Left.hasInstance(Right(1)); // ==> false
*
* ---
* category : Comparing and Testing
* ~belongsTo: makeInstance
* type: |
* (Variant) => Boolean
*
* ~belongsTo: makeInstance
*/
hasInstance(value) {
return Boolean(value)
@@ -217,12 +135,10 @@ instead to check if a value belongs to the ADT variant.`);
// --[ ADT Implementation ]--------------------------------------------
/*~
* ---
* category : Constructing Data Structures
* stability : experimental
* authors:
* - Quildreen Motta
*
*
* stability: experimental
* type: |
* (String, Object (Array String)) => ADT
*/
@@ -235,31 +151,15 @@ const data = (typeId, patterns) => {
[TYPE]: typeId,
/*~
* The variants present in this ADT.
*
* ---
* category: Members
* type: Array Variant
* ~belongsTo: ADTNamespace
*/
variants: values(variants),
/*~
* Checks if a value belongs to this ADT.
*
* Values are considered to belong to an ADT if they have the same
* `Symbol.for('@@folktale:adt:type')` property as the ADT's.
*
* If you don't want a structural check, you can test whether the
* ADT is in the prototype chain of the value, but keep in mind that
* this does not work cross-realm.
*
* ---
* category : Comparing and Testing
* ~belongsTo: ADTNamespace
* type: |
* ADT.(Variant) -> Boolean
*
* ~belongsTo: ADTNamespace
*/
hasInstance(value) {
return Boolean(value)
@@ -271,15 +171,9 @@ const data = (typeId, patterns) => {
};
/*~
* ---
* category : Data Structures
* ~belongsTo : data
*/
/*~~belongsTo : data */
const ADT = {
/*~
* ---
* category : Extending ADTs
* type: |
* ADT . (...(Variant, ADT) => Any) => ADT
*/
@@ -13,9 +13,6 @@ const { tagSymbol, typeSymbol } = require('./data');
// --[ Helpers ]--------------------------------------------------------
/*~
* Returns a string representing `key: value` pairs separated by
* commas.
* ---
* type: (Object Any) => String
*/
const objectToKeyValuePairs = (object) =>
@@ -24,47 +21,35 @@ const objectToKeyValuePairs = (object) =>
.join(', ');
/*~
* Returns a presentation of a plain object.
* ---
* type: (Object Any).() => String
*/
const plainObjectToString = function() {
return `{ ${objectToKeyValuePairs(this)} }`;
};
/*~
* Returns a representation of an array.
* ---
* type: (Array Any).() => String
*/
const arrayToString = function() {
return `[${this.map(showValue).join(', ')}]`;
};
/*~
* Returns a representation of a function's name.
* ---
* type: (Function) => String
*/
const functionNameToString = (fn) => fn.name !== '' ? `: ${fn.name}` : '';
/*~
* Returns a representation of a function.
* ---
* type: (Function) => String
*/
const functionToString = (fn) => `[Function${functionNameToString(fn)}]`;
/*~
* Returns a representation of a null value.
* ---
* type: () => String
*/
const nullToString = () => 'null';
/*~
* Returns a representation of any JS object.
* ---
* type: (Null | Object Any) => String
*/
const objectToString = (object) =>
@@ -75,8 +60,6 @@ const objectToString = (object) =>
/*~
* Returns a representation of any JS value.
* ---
* type: (Any) => String
*/
const showValue = (value) =>
@@ -89,16 +72,14 @@ const showValue = (value) =>
// --[ Implementation ]------------------------------------------------
/*~
* ---
* category: Derivation
* stability: experimental
* authors:
* - "@boris-marinov"
*
* type: |
* (Variant, ADT) => Void
*/
const show = (variant, adt) => { // eslint-disable-line max-statements
const debugRepresentation = (variant, adt) => { // eslint-disable-line max-statements
const typeName = adt[typeSymbol];
const variantName = `${adt[typeSymbol]}.${variant.prototype[tagSymbol]}`;
@@ -108,9 +89,6 @@ const show = (variant, adt) => { // eslint-disable-line max-statements
// (regular JavaScript representations)
/*~
* Returns a textual representation of the ADT.
* ---
* category: Debug Representation
* stability: experimental
* authors:
* - "@boris-marinov"
@@ -121,9 +99,6 @@ const show = (variant, adt) => { // eslint-disable-line max-statements
adt.toString = () => typeName;
/*~
* Returns a textual representation of the Variant.
* ---
* category: Debug Representation
* stability: experimental
* authors:
* - "@boris-marinov"
@@ -134,9 +109,6 @@ const show = (variant, adt) => { // eslint-disable-line max-statements
variant.toString = () => variantName;
/*~
* Returns a textual representation of the ADT instance.
* ---
* category: Debug Representation
* stability: experimental
* authors:
* - "@boris-marinov"
@@ -157,4 +129,4 @@ const show = (variant, adt) => { // eslint-disable-line max-statements
};
// --[ Exports ]-------------------------------------------------------
module.exports = show;
module.exports = debugRepresentation;
Oops, something went wrong.

0 comments on commit f0dd120

Please sign in to comment.