Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. # Preconditions for Node.js
  2. [![Build Status](https://secure.travis-ci.org/MathieuTurcotte/node-precond.png?branch=master)](https://travis-ci.org/MathieuTurcotte/node-precond)
  3. [![NPM version](https://badge.fury.io/js/precond.png)](http://badge.fury.io/js/precond)
  4. Precondition checks for Node.js inspired by [Guava's precondition checking
  5. utilities](https://code.google.com/p/guava-libraries/wiki/PreconditionsExplained).
  6. ## Installation
  7. ```
  8. npm install precond
  9. ```
  10. ## Unit tests
  11. ```
  12. npm test
  13. ```
  14. ## Overview
  15. Precond provides a set of functions to verify arguments and state correctness
  16. It lets you rewrite constructs like the following
  17. ```js
  18. if (!this.isConnected) {
  19. throw new Error('Client should be connected before calling X.');
  20. }
  21. ```
  22. into a more compact and declarative check bellow.
  23. ```js
  24. precond.checkState(this.isConnected, 'Client should be ...');
  25. ```
  26. **Note that even though the throw statement is wrapped in a function, the call
  27. stack will still start from the calling function. So the previous examples would
  28. both produce the same stack trace.**
  29. All arguments after the message will be used to format the actual error
  30. message that will be thrown.
  31. The following precondition checks are provded:
  32. - checkArgument(value, [messageFormat, [formatArgs, ...]])
  33. - checkState(value, [messageFormat, [formatArgs, ...]])
  34. - checkIsDef(value, [messageFormat, [formatArgs, ...]]) -> value
  35. - checkIsDefAndNotNull(value, [messageFormat, [formatArgs, ...]]) -> value
  36. - checkIsString(value, [messageFormat, [formatArgs, ...]]) -> value
  37. - checkIsArray(value, [messageFormat, [formatArgs, ...]]) -> value
  38. - checkIsNumber(value, [messageFormat, [formatArgs, ...]]) -> value
  39. - checkIsBoolean(value, [messageFormat, [formatArgs, ...]]) -> value
  40. - checkIsFunction(value, [messageFormat, [formatArgs, ...]]) -> value
  41. - checkIsObject(value, [messageFormat, [formatArgs, ...]]) -> value
  42. ## API
  43. ### Static functions
  44. #### precond.checkArgument(value, [messageFormat, [formatArgs, ...]])
  45. - value: the value that is required to be truthy
  46. - messageFormat: error message format template
  47. - formatArgs: arguments to be substituted into the message template
  48. Ensures that value is true. Throws an `IllegalArgumentError` if value
  49. is false.
  50. #### precond.checkState(value, [messageFormat, [formatArgs, ...]])
  51. - value: the value that is required to be truthy
  52. - messageFormat: error message format template
  53. - formatArgs: arguments to be substituted into the message template
  54. Ensures that value is true. Throws an `IllegalStateError` if value
  55. is false.
  56. #### precond.checkIsDef(value, [messageFormat, [formatArgs, ...]])
  57. - value: the value that is required to be defined
  58. - messageFormat: error message format template
  59. - formatArgs: arguments to be substituted into the message template
  60. Ensures that value is defined (could be null). Throws an
  61. `IllegalArgumentError` if value is undefined. Returns the value of
  62. the value that was validated.
  63. #### precond.checkIsDefAndNotNull(value, [messageFormat, [formatArgs, ...]])
  64. - value: the value that is required to be defined and not null
  65. - messageFormat: error message format template
  66. - formatArgs: arguments to be substituted into the message template
  67. Ensures that value is defined and not null. Throws an
  68. `IllegalArgumentError` if value is undefined or null. Returns the value of
  69. the value that was validated.
  70. #### precond.checkIsString(value, [messageFormat, [formatArgs, ...]])
  71. - value: the value that is required to be a string
  72. - messageFormat: error message format template
  73. - formatArgs: arguments to be substituted into the message template
  74. Ensures that value is a string or a String object. Throws an
  75. `IllegalArgumentError` if value isn't a string. Returns the value of
  76. the value that was validated.
  77. #### precond.checkIsArray(value, [messageFormat, [formatArgs, ...]])
  78. - value: the value that is required to be an array
  79. - messageFormat: error message format template
  80. - formatArgs: arguments to be substituted into the message template
  81. Ensures that value is an array. Throws an `IllegalArgumentError` if
  82. value isn't an array. Returns the value of the value that was
  83. validated.
  84. #### precond.checkIsNumber(value, [messageFormat, [formatArgs, ...]])
  85. - value: the value that is required to be a number
  86. - messageFormat: error message format template
  87. - formatArgs: arguments to be substituted into the message template
  88. Ensures that value is a number. Throws an `IllegalArgumentError` if
  89. value isn't a number. Returns the value of the value that was
  90. validated.
  91. #### precond.checkIsBoolean(value, [messageFormat, [formatArgs, ...]])
  92. - value: the value that is required to be a boolean
  93. - messageFormat: error message format template
  94. - formatArgs: arguments to be substituted into the message template
  95. Ensures that value is a boolean. Throws an `IllegalArgumentError` if
  96. value isn't a boolean. Returns the value of the value that was
  97. validated.
  98. #### precond.checkIsFunction(value, [messageFormat, [formatArgs, ...]])
  99. - value: the value that is required to be a function
  100. - messageFormat: error message format template
  101. - formatArgs: arguments to be substituted into the message template
  102. Ensures that value is a function. Throws an `IllegalArgumentError` if
  103. value isn't a function. Returns the value of the value that was
  104. validated.
  105. #### precond.checkIsObject(value, [messageFormat, [formatArgs, ...]])
  106. - value: the value that is required to be an object
  107. - messageFormat: error message format template
  108. - formatArgs: arguments to be substituted into the message template
  109. Ensures that value is an object. Throws an `IllegalArgumentError` if
  110. value isn't an object. Returns the value of the value that was
  111. validated.
  112. ### Class precond.IllegalArgumentError
  113. Extends `Error` and is thrown to signal illegal arguments.
  114. ### Class precond.IllegalStateError
  115. Extends `Error` and is thrown to signal that the program or object has reached
  116. an illegal state.
  117. ## License
  118. This code is free to use under the terms of the [MIT license](http://mturcotte.mit-license.org/).