Skip to content

Commit

Permalink
✨ Add Trail#ifNull(fallback) method
Browse files Browse the repository at this point in the history
  • Loading branch information
skerit committed Feb 26, 2024
1 parent 82bbd70 commit 07130bb
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.9.3 (WIP)

* Add `Trail#ifNull(fallback)` method

## 0.9.2 (2024-02-25)

* Clear the entire `Develry.Request.cache` as soon as a non-GET request is made
Expand Down
66 changes: 61 additions & 5 deletions lib/trail.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const CHAIN = Symbol('chain'),
GET_OR_EVALUATE = Symbol('getOrEvaluate');
GET_OR_EVALUATE = Symbol('getOrEvaluate'),
HAS_FALLBACK = Symbol('hasFallback'),
FALLBACK = Symbol('fallback');

/**
* Class that represents a path
Expand All @@ -21,6 +23,10 @@ const Trail = Fn.inherits('Develry.Placeholder', function Trail(chain) {
// The path pieces
this[CHAIN] = chain;

// There is no fallback by default
this[HAS_FALLBACK] = false;
this[FALLBACK] = undefined;

// Is this a root path?
this.is_root = chain[0] === '';

Expand All @@ -45,6 +51,12 @@ Trail.setStatic(function unDry(value) {
let result = Object.create(this.prototype);
result[CHAIN] = value.chain;
result.is_root = value.is_root;

if (value.fallback) {
result[HAS_FALLBACK] = true;
result[FALLBACK] = value.fallback.value;
}

return result;
});

Expand Down Expand Up @@ -133,7 +145,7 @@ Trail.setStatic(function fromSlash(path) {
*
* @author Jelle De Loecker <[email protected]>
* @since 0.9.0
* @version 0.9.0
* @version 0.9.3
*/
Trail.setMethod(function toDry() {

Expand All @@ -145,18 +157,58 @@ Trail.setMethod(function toDry() {
value.is_root = true;
}

if (this[HAS_FALLBACK]) {
value.fallback = {value: this[FALLBACK]};
}

return {
value: value,
};
});

/**
* Create a shallow clone
*
* @author Jelle De Loecker <[email protected]>
* @since 0.9.3
* @version 0.9.3
*
* @return {Trail}
*/
Trail.setMethod(function shallowClone() {
let result = Object.create(this.constructor.prototype);
result[CHAIN] = this[CHAIN].slice(0);
result.is_root = this.is_root;
result[HAS_FALLBACK] = this[HAS_FALLBACK];
result[FALLBACK] = this[FALLBACK];
return result;
});

/**
* Set the fallback value for this trail.
* This will then be returned in case the value is null or undefined.
*
* @author Jelle De Loecker <[email protected]>
* @since 0.9.3
* @version 0.9.3
*
* @param {*} fallback
*
* @return {Trail}
*/
Trail.setMethod(function ifNull(fallback) {
this[HAS_FALLBACK] = true;
this[FALLBACK] = fallback;
return this;
});

/**
* Get the value of the given path.
* If `evaluate` is true and the last piece is a function, it will be called.
*
* @author Jelle De Loecker <[email protected]>
* @since 0.9.0
* @version 0.9.0
* @version 0.9.3
*
* @param {Object} context
* @param {boolean} evaluate
Expand All @@ -180,14 +232,14 @@ Trail.setMethod([GET_OR_EVALUATE], function internalGetOrEvaluate(context, evalu
for (index = 0; index < length; index++) {

if (context == null) {
return;
break;
}

key = chain[index];
value = context[key];

if (value == null) {
return;
break;
}

if (evaluate && index == last && typeof value == 'function') {
Expand All @@ -197,6 +249,10 @@ Trail.setMethod([GET_OR_EVALUATE], function internalGetOrEvaluate(context, evalu
}
}

if (value == null && this[HAS_FALLBACK]) {
value = this[FALLBACK];
}

return value;
});

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "protoblast",
"description": "Native object expansion library",
"version": "0.9.2",
"version": "0.9.3-alpha",
"author": "Jelle De Loecker <[email protected]>",
"keywords": [
"prototype",
Expand Down

0 comments on commit 07130bb

Please sign in to comment.