Example #1
0
function when (path, conditions = { 'true': truthy, 'false': otherwise }) {
  const getValue = getCompiler(path)

  // prepare the output conditions
  let otherwisePath = null
  const outputConditions = {}
  if (Array.isArray(conditions)) {
    conditions.forEach((condition) => {
      outputConditions[condition] = condition
    })
  } else {
    for (let path in conditions) {
      outputConditions[path] = conditions[path]
      otherwisePath = otherwisePath || (conditions[path] === otherwise && path)
    }
  }
  if (!otherwisePath) {
    outputConditions['otherwise'] = otherwise
    otherwisePath = 'otherwise'
  }

  // test the getter returned value
  const whenTest = (args, value) => {
    // treat objects with no keys as falsy
    if (value && typeof value === 'object' && Object.keys(value).length === 0) {
      value = false
    }

    let outputPath

    for (let path in outputConditions) {
      let test = outputConditions[path]
      if (test !== otherwise &&
        ((test === value) || (test === truthy && value) || (test === falsy && !value))) {
        outputPath = path
        break
      }
    }

    args.output[outputPath || otherwisePath]()
  }

  // define the action
  let action = function whenRead (args) {
    let value = getValue(args)
    if (value && typeof value.then === 'function') {
      value.then((val) => whenTest(args, val)).catch((error) => {
        console.error(`${action.displayName} caught an error whilst getting a value to test`, error)
      })
    } else {
      whenTest(args, value)
    }
  }

  action.outputs = Object.keys(outputConditions)

  action.displayName = `addons.when(${toDisplayName(path, getValue)})`

  return action
}
function patch(url, dataPath) {

  var urlGetters = convertToGetters(url);
  var getValue = getCompiler(dataPath);

  function action(args) {
    var services = args.services;
    var httpPath = args['cerebral-module-http'];
    var http = httpPath.reduce(function (services, key) {
      return services[key];
    }, services);
    var output = args.output;

    if (!http) {
      throw 'Http action factories require \'cerebral-module-http\' module to be added to controller or current module';
    }

    var fullUrl = createFullUrl(urlGetters, args);

    http.patch(fullUrl, getValue(args))
      .then(output.success)
      .catch(output.error);
  }

  action.async = true;
  action.displayName = 'http.patch (' + ([].slice.call(arguments).join(', ')) + ')';

  return action;

}
Example #3
0
export default function (fromPath, ...toPaths) {
  const getValue = getCompiler(fromPath)
  const setValues = toPaths.map((toPath) => setCompiler(toPath))

  const copyTo = (setters, args, value, async) => {
    if (setters.length === 0) {
      if (async) {
        args.output.success(value)
      }
    } else {
      const response = setters[0](args, value)
      if (response && typeof response.then === 'function') {
        response.then((val) => copyTo(setters.slice(1), args, val, true)).catch(args.output.error)
      } else {
        copyTo(setters.slice(1), args, response, async)
      }
    }
  }

  const copy = function copyFrom (args) {
    let value = getValue(args)
    if (value && typeof value.then === 'function') {
      copy.async = true
      value.then((val) => copyTo(setValues, args, val, true)).catch(args.output.error)
    } else {
      copyTo(setValues, args, value)
    }
  }

  copy.displayName = `operators.copy(${toDisplayName(fromPath, getValue)}, ${toPaths.map((path, index) =>
    toDisplayName(path, setValues[index])).join(', ')})`

  return copy
}
Example #4
0
export default function (path) {
  const getter = getCompiler(path)

  const not = function not (args) {
    return !isTruthy(getter(args))
  }

  not.displayName = `not(${toDisplayName(path, getter)})`

  return not
}
Example #5
0
 const getters = paths.map((path) => getCompiler(path))
 urlGetters = url.map(function (urlPart) {
   return {
     fn: getCompiler(urlPart),
     urlPart: urlPart
   };
 });