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.

conventions.js 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. 'use strict'
  2. /**
  3. * Ponyfill for `Array.prototype.find` which is only available in ES6 runtimes.
  4. *
  5. * Works with anything that has a `length` property and index access properties, including NodeList.
  6. *
  7. * @template {unknown} T
  8. * @param {Array<T> | ({length:number, [number]: T})} list
  9. * @param {function (item: T, index: number, list:Array<T> | ({length:number, [number]: T})):boolean} predicate
  10. * @param {Partial<Pick<ArrayConstructor['prototype'], 'find'>>?} ac `Array.prototype` by default,
  11. * allows injecting a custom implementation in tests
  12. * @returns {T | undefined}
  13. *
  14. * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
  15. * @see https://tc39.es/ecma262/multipage/indexed-collections.html#sec-array.prototype.find
  16. */
  17. function find(list, predicate, ac) {
  18. if (ac === undefined) {
  19. ac = Array.prototype;
  20. }
  21. if (list && typeof ac.find === 'function') {
  22. return ac.find.call(list, predicate);
  23. }
  24. for (var i = 0; i < list.length; i++) {
  25. if (Object.prototype.hasOwnProperty.call(list, i)) {
  26. var item = list[i];
  27. if (predicate.call(undefined, item, i, list)) {
  28. return item;
  29. }
  30. }
  31. }
  32. }
  33. /**
  34. * "Shallow freezes" an object to render it immutable.
  35. * Uses `Object.freeze` if available,
  36. * otherwise the immutability is only in the type.
  37. *
  38. * Is used to create "enum like" objects.
  39. *
  40. * @template T
  41. * @param {T} object the object to freeze
  42. * @param {Pick<ObjectConstructor, 'freeze'> = Object} oc `Object` by default,
  43. * allows to inject custom object constructor for tests
  44. * @returns {Readonly<T>}
  45. *
  46. * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze
  47. */
  48. function freeze(object, oc) {
  49. if (oc === undefined) {
  50. oc = Object
  51. }
  52. return oc && typeof oc.freeze === 'function' ? oc.freeze(object) : object
  53. }
  54. /**
  55. * Since we can not rely on `Object.assign` we provide a simplified version
  56. * that is sufficient for our needs.
  57. *
  58. * @param {Object} target
  59. * @param {Object | null | undefined} source
  60. *
  61. * @returns {Object} target
  62. * @throws TypeError if target is not an object
  63. *
  64. * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign
  65. * @see https://tc39.es/ecma262/multipage/fundamental-objects.html#sec-object.assign
  66. */
  67. function assign(target, source) {
  68. if (target === null || typeof target !== 'object') {
  69. throw new TypeError('target is not an object')
  70. }
  71. for (var key in source) {
  72. if (Object.prototype.hasOwnProperty.call(source, key)) {
  73. target[key] = source[key]
  74. }
  75. }
  76. return target
  77. }
  78. /**
  79. * All mime types that are allowed as input to `DOMParser.parseFromString`
  80. *
  81. * @see https://developer.mozilla.org/en-US/docs/Web/API/DOMParser/parseFromString#Argument02 MDN
  82. * @see https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#domparsersupportedtype WHATWG HTML Spec
  83. * @see DOMParser.prototype.parseFromString
  84. */
  85. var MIME_TYPE = freeze({
  86. /**
  87. * `text/html`, the only mime type that triggers treating an XML document as HTML.
  88. *
  89. * @see DOMParser.SupportedType.isHTML
  90. * @see https://www.iana.org/assignments/media-types/text/html IANA MimeType registration
  91. * @see https://en.wikipedia.org/wiki/HTML Wikipedia
  92. * @see https://developer.mozilla.org/en-US/docs/Web/API/DOMParser/parseFromString MDN
  93. * @see https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#dom-domparser-parsefromstring WHATWG HTML Spec
  94. */
  95. HTML: 'text/html',
  96. /**
  97. * Helper method to check a mime type if it indicates an HTML document
  98. *
  99. * @param {string} [value]
  100. * @returns {boolean}
  101. *
  102. * @see https://www.iana.org/assignments/media-types/text/html IANA MimeType registration
  103. * @see https://en.wikipedia.org/wiki/HTML Wikipedia
  104. * @see https://developer.mozilla.org/en-US/docs/Web/API/DOMParser/parseFromString MDN
  105. * @see https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#dom-domparser-parsefromstring */
  106. isHTML: function (value) {
  107. return value === MIME_TYPE.HTML
  108. },
  109. /**
  110. * `application/xml`, the standard mime type for XML documents.
  111. *
  112. * @see https://www.iana.org/assignments/media-types/application/xml IANA MimeType registration
  113. * @see https://tools.ietf.org/html/rfc7303#section-9.1 RFC 7303
  114. * @see https://en.wikipedia.org/wiki/XML_and_MIME Wikipedia
  115. */
  116. XML_APPLICATION: 'application/xml',
  117. /**
  118. * `text/html`, an alias for `application/xml`.
  119. *
  120. * @see https://tools.ietf.org/html/rfc7303#section-9.2 RFC 7303
  121. * @see https://www.iana.org/assignments/media-types/text/xml IANA MimeType registration
  122. * @see https://en.wikipedia.org/wiki/XML_and_MIME Wikipedia
  123. */
  124. XML_TEXT: 'text/xml',
  125. /**
  126. * `application/xhtml+xml`, indicates an XML document that has the default HTML namespace,
  127. * but is parsed as an XML document.
  128. *
  129. * @see https://www.iana.org/assignments/media-types/application/xhtml+xml IANA MimeType registration
  130. * @see https://dom.spec.whatwg.org/#dom-domimplementation-createdocument WHATWG DOM Spec
  131. * @see https://en.wikipedia.org/wiki/XHTML Wikipedia
  132. */
  133. XML_XHTML_APPLICATION: 'application/xhtml+xml',
  134. /**
  135. * `image/svg+xml`,
  136. *
  137. * @see https://www.iana.org/assignments/media-types/image/svg+xml IANA MimeType registration
  138. * @see https://www.w3.org/TR/SVG11/ W3C SVG 1.1
  139. * @see https://en.wikipedia.org/wiki/Scalable_Vector_Graphics Wikipedia
  140. */
  141. XML_SVG_IMAGE: 'image/svg+xml',
  142. })
  143. /**
  144. * Namespaces that are used in this code base.
  145. *
  146. * @see http://www.w3.org/TR/REC-xml-names
  147. */
  148. var NAMESPACE = freeze({
  149. /**
  150. * The XHTML namespace.
  151. *
  152. * @see http://www.w3.org/1999/xhtml
  153. */
  154. HTML: 'http://www.w3.org/1999/xhtml',
  155. /**
  156. * Checks if `uri` equals `NAMESPACE.HTML`.
  157. *
  158. * @param {string} [uri]
  159. *
  160. * @see NAMESPACE.HTML
  161. */
  162. isHTML: function (uri) {
  163. return uri === NAMESPACE.HTML
  164. },
  165. /**
  166. * The SVG namespace.
  167. *
  168. * @see http://www.w3.org/2000/svg
  169. */
  170. SVG: 'http://www.w3.org/2000/svg',
  171. /**
  172. * The `xml:` namespace.
  173. *
  174. * @see http://www.w3.org/XML/1998/namespace
  175. */
  176. XML: 'http://www.w3.org/XML/1998/namespace',
  177. /**
  178. * The `xmlns:` namespace
  179. *
  180. * @see https://www.w3.org/2000/xmlns/
  181. */
  182. XMLNS: 'http://www.w3.org/2000/xmlns/',
  183. })
  184. exports.assign = assign;
  185. exports.find = find;
  186. exports.freeze = freeze;
  187. exports.MIME_TYPE = MIME_TYPE;
  188. exports.NAMESPACE = NAMESPACE;