s1seven / schema-tools

Tools to create, validate and render certificates using Material Identity JSON schemas
https://materialidentity.org/
Apache License 2.0
1 stars 1 forks source link

feat: return `false` for get function in packages/generate-html/src/index.ts #153

Closed stiebitzhofer closed 2 years ago

stiebitzhofer commented 2 years ago

Description

What keywords did you search in schema-tools issues before filing this one ?

get

Is this a BUG REPORT or FEATURE REQUEST ?

FEATURE REQUEST

What you expected to happen :

Return false if the result of get is undefined. By this an if statement in a handlebars template evaluates the result of the function properly.

get: function (object: Record<string, unknown>, path: string | string[]) {
  path = typeof path === 'string' ? path.split(',').map((val) => val.trim()) : path;
  const result = get(object, path);
  if (result) {
    return new SafeString(result);
  } else {
    return false;
  }
},      
getlarge commented 2 years ago

Returning a boolean instead of a string is not exactly what everyone would expect from this sort of function. So i suggest to do like the lodash.get method instead (which is wrapped by that Handlebars helper) and provide a default value as a 3rd argument of the helper.

get: function (object: Record<string, unknown>, path: string | string[], defaultValue = undefined) {
  path = typeof path === 'string' ? path.split(',').map((val) => val.trim()) : path;
  const result = get(object, path);
  return result ? new SafeString(result) : defaultValue;
},   
stiebitzhofer commented 2 years ago

I am fine with that approach as well, it will solve my issue. Will test it locally.