Skip to content

Commit

Permalink
✨ Add Optional value-wrapper class
Browse files Browse the repository at this point in the history
  • Loading branch information
skerit committed Apr 26, 2024
1 parent f1671c9 commit a12004f
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* Fix comments breaking the SSE request handler
* Add `Blast.isAppleWebkit` boolean
* Add `Blast.environment` string property and `isProduction`, `isDevelopment` and `isStaging` booleans
* Add `Optional` value-wrapper class

## 0.9.2 (2024-02-25)

Expand Down
1 change: 1 addition & 0 deletions lib/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,7 @@ function BlastInit(modifyPrototype) {
'Error',
'Placeholder',
'Trail',
'Optional',
'Informer',
'State',
'Request',
Expand Down
134 changes: 134 additions & 0 deletions lib/optional.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
const VALUE = Symbol('value'),
CALLBACKS = Symbol('callbacks');

/**
* An optional value
*
* @constructor
*
* @author Jelle De Loecker <[email protected]>
* @since 0.9.3
* @version 0.9.3
*
* @param {*} value
*/
const Optional = Fn.inherits('Develry.Placeholder', function Optional(value) {
this[VALUE] = value;
this[CALLBACKS] = [];
});

/**
* Undry the value
*
* @author Jelle De Loecker <[email protected]>
* @since 0.9.3
* @version 0.9.3
*
* @param {Object} value
*
* @return {Optional}
*/
Optional.setStatic(function unDry(value) {
let result = Object.create(this.prototype);
result[VALUE] = value.value;
return result;
});

/**
* Get the current value
*
* @author Jelle De Loecker <[email protected]>
* @since 0.9.3
* @version 0.9.3
*
* @return {*}
*/
Optional.setProperty(function value() {
return this[VALUE];
}, function setValue(new_value) {
this[VALUE] = new_value;

let callbacks = this[CALLBACKS],
i;

for (i = 0; i < callbacks.length; i++) {
callbacks[i](new_value);
}
});

/**
* Return the serialized json-dry representation
*
* @author Jelle De Loecker <[email protected]>
* @since 0.9.3
* @version 0.9.3
*/
Optional.setMethod(function toDry() {

let value = {
value: this[VALUE]
};

return {
value: value
};
});

/**
* Add a listener
*
* @author Jelle De Loecker <[email protected]>
* @since 0.9.3
* @version 0.9.3
*
* @param {Function} callback
*/
Optional.setMethod(function onChange(callback) {
this[CALLBACKS].push(callback);
});

/**
* Is there a value present?
*
* @author Jelle De Loecker <[email protected]>
* @since 0.9.3
* @version 0.9.3
*
* @return {boolean}
*/
Optional.setMethod(function isPresent() {
return this[VALUE] != null;
});

/**
* Get the current value if it is present of the given fallback value if it is not
*
* @author Jelle De Loecker <[email protected]>
* @since 0.9.3
* @version 0.9.3
*
* @param {*} fallback
*
* @return {*}
*/
Optional.setMethod(function orElse(fallback) {

if (this.isPresent()) {
return this[VALUE];
}

return fallback;
});

/**
* This method should return the actual value
*
* @author Jelle De Loecker <[email protected]>
* @since 0.9.3
* @version 0.9.3
*
* @return {*}
*/
Optional.setMethod(function getResolvedValue() {
return this[VALUE];
});

0 comments on commit a12004f

Please sign in to comment.