lib/handlebar-helpers.js

  1. // Copyright 2018 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. 'use strict';
  15. /**
  16. * @namespace templateHelpers
  17. */
  18. module.exports = {
  19. notEmptyObject: notEmptyObject,
  20. json: json,
  21. isNotDefaultStatusCode: isNotDefaultStatusCode
  22. }
  23. /**
  24. * determines if the given object is empty or not
  25. * @function notEmptyObject
  26. * @memberof templateHelpers
  27. * @instance
  28. * @param {object} obj object to be evaluated
  29. * @return {boolean}
  30. */
  31. function notEmptyObject(obj) {
  32. if (arguments.length < 1) {
  33. throw new Error('Handlebars Helper \'notEmptyObject\' needs 1 parameter');
  34. }
  35. return obj !== undefined && Object.keys(obj).length !== 0
  36. }
  37. /**
  38. * stringifies the given JSON object
  39. * @function json
  40. * @memberof templateHelpers
  41. * @instance
  42. * @param {object} obj JSON object to be stringified
  43. * @return {string}
  44. */
  45. function json(obj) {
  46. if (arguments.length < 1) {
  47. throw new Error('Handlebars Helper \'json\' needs 1 parameter');
  48. }
  49. return JSON.stringify(obj)
  50. }
  51. /**
  52. * determines if the given status code is the 'default' status
  53. * @function isNotDefaultStatusCode
  54. * @memberof templateHelpers
  55. * @instance
  56. * @param {(string | number)} code status code to check
  57. * @return {boolean}
  58. */
  59. function isNotDefaultStatusCode(code) {
  60. if (arguments.length < 1) {
  61. throw new Error(
  62. 'Handlebars Helper \'isNotDefaultStatusCode\' needs 1 parameter');
  63. }
  64. return code !== 'default'
  65. }