web 3d图形渲染器
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.

1039 lines
40 KiB

  1. [![Build status][nix-build-image]][nix-build-url]
  2. [![Windows status][win-build-image]][win-build-url]
  3. ![Transpilation status][transpilation-image]
  4. [![npm version][npm-image]][npm-url]
  5. # es5-ext
  6. ## ECMAScript 5 extensions
  7. ### (with respect to ECMAScript 6 standard)
  8. Shims for upcoming ES6 standard and other goodies implemented strictly with ECMAScript conventions in mind.
  9. It's designed to be used in compliant ECMAScript 5 or ECMAScript 6 environments. Older environments are not supported, although most of the features should work with correct ECMAScript 5 shim on board.
  10. When used in ECMAScript 6 environment, native implementation (if valid) takes precedence over shims.
  11. ### Installation
  12. $ npm install es5-ext
  13. To port it to Browser or any other (non CJS) environment, use your favorite CJS bundler. No favorite yet? Try: [Browserify](http://browserify.org/), [Webmake](https://github.com/medikoo/modules-webmake) or [Webpack](http://webpack.github.io/)
  14. ### Usage
  15. #### ECMAScript 6 features
  16. You can force ES6 features to be implemented in your environment, e.g. following will assign `from` function to `Array` (only if it's not implemented already).
  17. ```javascript
  18. require("es5-ext/array/from/implement");
  19. Array.from("foo"); // ['f', 'o', 'o']
  20. ```
  21. You can also access shims directly, without fixing native objects. Following will return native `Array.from` if it's available and fallback to shim if it's not.
  22. ```javascript
  23. var aFrom = require("es5-ext/array/from");
  24. aFrom("foo"); // ['f', 'o', 'o']
  25. ```
  26. If you want to use shim unconditionally (even if native implementation exists) do:
  27. ```javascript
  28. var aFrom = require("es5-ext/array/from/shim");
  29. aFrom("foo"); // ['f', 'o', 'o']
  30. ```
  31. ##### List of ES6 shims
  32. It's about properties introduced with ES6 and those that have been updated in new spec.
  33. - `Array.from` -> `require('es5-ext/array/from')`
  34. - `Array.of` -> `require('es5-ext/array/of')`
  35. - `Array.prototype.concat` -> `require('es5-ext/array/#/concat')`
  36. - `Array.prototype.copyWithin` -> `require('es5-ext/array/#/copy-within')`
  37. - `Array.prototype.entries` -> `require('es5-ext/array/#/entries')`
  38. - `Array.prototype.fill` -> `require('es5-ext/array/#/fill')`
  39. - `Array.prototype.filter` -> `require('es5-ext/array/#/filter')`
  40. - `Array.prototype.find` -> `require('es5-ext/array/#/find')`
  41. - `Array.prototype.findIndex` -> `require('es5-ext/array/#/find-index')`
  42. - `Array.prototype.keys` -> `require('es5-ext/array/#/keys')`
  43. - `Array.prototype.map` -> `require('es5-ext/array/#/map')`
  44. - `Array.prototype.slice` -> `require('es5-ext/array/#/slice')`
  45. - `Array.prototype.splice` -> `require('es5-ext/array/#/splice')`
  46. - `Array.prototype.values` -> `require('es5-ext/array/#/values')`
  47. - `Array.prototype[@@iterator]` -> `require('es5-ext/array/#/@@iterator')`
  48. - `Math.acosh` -> `require('es5-ext/math/acosh')`
  49. - `Math.asinh` -> `require('es5-ext/math/asinh')`
  50. - `Math.atanh` -> `require('es5-ext/math/atanh')`
  51. - `Math.cbrt` -> `require('es5-ext/math/cbrt')`
  52. - `Math.clz32` -> `require('es5-ext/math/clz32')`
  53. - `Math.cosh` -> `require('es5-ext/math/cosh')`
  54. - `Math.exmp1` -> `require('es5-ext/math/expm1')`
  55. - `Math.fround` -> `require('es5-ext/math/fround')`
  56. - `Math.hypot` -> `require('es5-ext/math/hypot')`
  57. - `Math.imul` -> `require('es5-ext/math/imul')`
  58. - `Math.log1p` -> `require('es5-ext/math/log1p')`
  59. - `Math.log2` -> `require('es5-ext/math/log2')`
  60. - `Math.log10` -> `require('es5-ext/math/log10')`
  61. - `Math.sign` -> `require('es5-ext/math/sign')`
  62. - `Math.signh` -> `require('es5-ext/math/signh')`
  63. - `Math.tanh` -> `require('es5-ext/math/tanh')`
  64. - `Math.trunc` -> `require('es5-ext/math/trunc')`
  65. - `Number.EPSILON` -> `require('es5-ext/number/epsilon')`
  66. - `Number.MAX_SAFE_INTEGER` -> `require('es5-ext/number/max-safe-integer')`
  67. - `Number.MIN_SAFE_INTEGER` -> `require('es5-ext/number/min-safe-integer')`
  68. - `Number.isFinite` -> `require('es5-ext/number/is-finite')`
  69. - `Number.isInteger` -> `require('es5-ext/number/is-integer')`
  70. - `Number.isNaN` -> `require('es5-ext/number/is-nan')`
  71. - `Number.isSafeInteger` -> `require('es5-ext/number/is-safe-integer')`
  72. - `Object.assign` -> `require('es5-ext/object/assign')`
  73. - `Object.keys` -> `require('es5-ext/object/keys')`
  74. - `Object.setPrototypeOf` -> `require('es5-ext/object/set-prototype-of')`
  75. - `Promise.prototype.finally` -> `require('es5-ext/promise/#/finally')`
  76. - `RegExp.prototype.match` -> `require('es5-ext/reg-exp/#/match')`
  77. - `RegExp.prototype.replace` -> `require('es5-ext/reg-exp/#/replace')`
  78. - `RegExp.prototype.search` -> `require('es5-ext/reg-exp/#/search')`
  79. - `RegExp.prototype.split` -> `require('es5-ext/reg-exp/#/split')`
  80. - `RegExp.prototype.sticky` -> Implement with `require('es5-ext/reg-exp/#/sticky/implement')`, use as function with `require('es5-ext/reg-exp/#/is-sticky')`
  81. - `RegExp.prototype.unicode` -> Implement with `require('es5-ext/reg-exp/#/unicode/implement')`, use as function with `require('es5-ext/reg-exp/#/is-unicode')`
  82. - `String.fromCodePoint` -> `require('es5-ext/string/from-code-point')`
  83. - `String.raw` -> `require('es5-ext/string/raw')`
  84. - `String.prototype.codePointAt` -> `require('es5-ext/string/#/code-point-at')`
  85. - `String.prototype.contains` -> `require('es5-ext/string/#/contains')`
  86. - `String.prototype.endsWith` -> `require('es5-ext/string/#/ends-with')`
  87. - `String.prototype.normalize` -> `require('es5-ext/string/#/normalize')`
  88. - `String.prototype.repeat` -> `require('es5-ext/string/#/repeat')`
  89. - `String.prototype.startsWith` -> `require('es5-ext/string/#/starts-with')`
  90. - `String.prototype[@@iterator]` -> `require('es5-ext/string/#/@@iterator')`
  91. #### Non ECMAScript standard features
  92. **es5-ext** provides also other utils, and implements them as if they were proposed for a standard. It mostly offers methods (not functions) which can directly be assigned to native prototypes:
  93. ```javascript
  94. Object.defineProperty(Function.prototype, "partial", {
  95. value: require("es5-ext/function/#/partial"),
  96. configurable: true,
  97. enumerable: false,
  98. writable: true
  99. });
  100. Object.defineProperty(Array.prototype, "flatten", {
  101. value: require("es5-ext/array/#/flatten"),
  102. configurable: true,
  103. enumerable: false,
  104. writable: true
  105. });
  106. Object.defineProperty(String.prototype, "capitalize", {
  107. value: require("es5-ext/string/#/capitalize"),
  108. configurable: true,
  109. enumerable: false,
  110. writable: true
  111. });
  112. ```
  113. See [es5-extend](https://github.com/wookieb/es5-extend#es5-extend), a great utility that automatically will extend natives for you.
  114. **Important:** Remember to **not** extend natives in scope of generic reusable packages (e.g. ones you intend to publish to npm). Extending natives is fine **only** if you're the _owner_ of the global scope, so e.g. in final project you lead development of.
  115. When you're in situation when native extensions are not good idea, then you should use methods indirectly:
  116. ```javascript
  117. var flatten = require("es5-ext/array/#/flatten");
  118. flatten.call([1, [2, [3, 4]]]); // [1, 2, 3, 4]
  119. ```
  120. for better convenience you can turn methods into functions:
  121. ```javascript
  122. var call = Function.prototype.call;
  123. var flatten = call.bind(require("es5-ext/array/#/flatten"));
  124. flatten([1, [2, [3, 4]]]); // [1, 2, 3, 4]
  125. ```
  126. You can configure custom toolkit (like [underscorejs](http://underscorejs.org/)), and use it throughout your application
  127. ```javascript
  128. var util = {};
  129. util.partial = call.bind(require("es5-ext/function/#/partial"));
  130. util.flatten = call.bind(require("es5-ext/array/#/flatten"));
  131. util.startsWith = call.bind(require("es5-ext/string/#/starts-with"));
  132. util.flatten([1, [2, [3, 4]]]); // [1, 2, 3, 4]
  133. ```
  134. As with native ones most methods are generic and can be run on any type of object.
  135. ## API
  136. ### Global extensions
  137. #### global _(es5-ext/global)_
  138. Object that represents global scope
  139. ### Array Constructor extensions
  140. #### from(arrayLike[, mapFn[, thisArg]]) _(es5-ext/array/from)_
  141. [_Introduced with ECMAScript 6_](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-array.from).
  142. Returns array representation of _iterable_ or _arrayLike_. If _arrayLike_ is an instance of array, its copy is returned.
  143. #### generate([length[, …fill]]) _(es5-ext/array/generate)_
  144. Generate an array of pre-given _length_ built of repeated arguments.
  145. #### isPlainArray(x) _(es5-ext/array/is-plain-array)_
  146. Returns true if object is plain array (not instance of one of the Array's extensions).
  147. #### of([…items]) _(es5-ext/array/of)_
  148. [_Introduced with ECMAScript 6_](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-array.of).
  149. Create an array from given arguments.
  150. #### toArray(obj) _(es5-ext/array/to-array)_
  151. Returns array representation of `obj`. If `obj` is already an array, `obj` is returned back.
  152. #### validArray(obj) _(es5-ext/array/valid-array)_
  153. Returns `obj` if it's an array, otherwise throws `TypeError`
  154. ### Array Prototype extensions
  155. #### arr.binarySearch(compareFn) _(es5-ext/array/#/binary-search)_
  156. In **sorted** list search for index of item for which _compareFn_ returns value closest to _0_.
  157. It's variant of binary search algorithm
  158. #### arr.clear() _(es5-ext/array/#/clear)_
  159. Clears the array
  160. #### arr.compact() _(es5-ext/array/#/compact)_
  161. Returns a copy of the context with all non-values (`null` or `undefined`) removed.
  162. #### arr.concat() _(es5-ext/array/#/concat)_
  163. [_Updated with ECMAScript 6_](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-array.prototype.concat).
  164. ES6's version of `concat`. Supports `isConcatSpreadable` symbol, and returns array of same type as the context.
  165. #### arr.contains(searchElement[, position]) _(es5-ext/array/#/contains)_
  166. Whether list contains the given value.
  167. #### arr.copyWithin(target, start[, end]) _(es5-ext/array/#/copy-within)_
  168. [_Introduced with ECMAScript 6_](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-array.copywithin).
  169. #### arr.diff(other) _(es5-ext/array/#/diff)_
  170. Returns the array of elements that are present in context list but not present in other list.
  171. #### arr.eIndexOf(searchElement[, fromIndex]) _(es5-ext/array/#/e-index-of)_
  172. _egal_ version of `indexOf` method. [_SameValueZero_](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-samevaluezero) logic is used for comparision
  173. #### arr.eLastIndexOf(searchElement[, fromIndex]) _(es5-ext/array/#/e-last-index-of)_
  174. _egal_ version of `lastIndexOf` method. [_SameValueZero_](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-samevaluezero) logic is used for comparision
  175. #### arr.entries() _(es5-ext/array/#/entries)_
  176. [_Introduced with ECMAScript 6_](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-array.prototype.entries).
  177. Returns iterator object, which traverses the array. Each value is represented with an array, where first value is an index and second is corresponding to index value.
  178. #### arr.exclusion([…lists]]) _(es5-ext/array/#/exclusion)_
  179. Returns the array of elements that are found only in one of the lists (either context list or list provided in arguments).
  180. #### arr.fill(value[, start, end]) _(es5-ext/array/#/fill)_
  181. [_Introduced with ECMAScript 6_](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-array.fill).
  182. #### arr.filter(callback[, thisArg]) _(es5-ext/array/#/filter)_
  183. [_Updated with ECMAScript 6_](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-array.filter).
  184. ES6's version of `filter`, returns array of same type as the context.
  185. #### arr.find(predicate[, thisArg]) _(es5-ext/array/#/find)_
  186. [_Introduced with ECMAScript 6_](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-array.find).
  187. Return first element for which given function returns true
  188. #### arr.findIndex(predicate[, thisArg]) _(es5-ext/array/#/find-index)_
  189. [_Introduced with ECMAScript 6_](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-array.findindex).
  190. Return first index for which given function returns true
  191. #### arr.first() _(es5-ext/array/#/first)_
  192. Returns value for first defined index
  193. #### arr.firstIndex() _(es5-ext/array/#/first-index)_
  194. Returns first declared index of the array
  195. #### arr.flatten() _(es5-ext/array/#/flatten)_
  196. Returns flattened version of the array
  197. #### arr.forEachRight(cb[, thisArg]) _(es5-ext/array/#/for-each-right)_
  198. `forEach` starting from last element
  199. #### arr.group(cb[, thisArg]) _(es5-ext/array/#/group)_
  200. Group list elements by value returned by _cb_ function
  201. #### arr.indexesOf(searchElement[, fromIndex]) _(es5-ext/array/#/indexes-of)_
  202. Returns array of all indexes of given value
  203. #### arr.intersection([…lists]) _(es5-ext/array/#/intersection)_
  204. Computes the array of values that are the intersection of all lists (context list and lists given in arguments)
  205. #### arr.isCopy(other) _(es5-ext/array/#/is-copy)_
  206. Returns true if both context and _other_ lists have same content
  207. #### arr.isUniq() _(es5-ext/array/#/is-uniq)_
  208. Returns true if all values in array are unique
  209. #### arr.keys() _(es5-ext/array/#/keys)_
  210. [_Introduced with ECMAScript 6_](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-array.prototype.keys).
  211. Returns iterator object, which traverses all array indexes.
  212. #### arr.last() _(es5-ext/array/#/last)_
  213. Returns value of last defined index
  214. #### arr.lastIndex() _(es5-ext/array/#/last)_
  215. Returns last defined index of the array
  216. #### arr.map(callback[, thisArg]) _(es5-ext/array/#/map)_
  217. [_Updated with ECMAScript 6_](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-array.map).
  218. ES6's version of `map`, returns array of same type as the context.
  219. #### arr.remove(value[, …valuen]) _(es5-ext/array/#/remove)_
  220. Remove values from the array
  221. #### arr.separate(sep) _(es5-ext/array/#/separate)_
  222. Returns array with items separated with `sep` value
  223. #### arr.slice(callback[, thisArg]) _(es5-ext/array/#/slice)_
  224. [_Updated with ECMAScript 6_](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-array.slice).
  225. ES6's version of `slice`, returns array of same type as the context.
  226. #### arr.someRight(cb[, thisArg]) _(es5-ext/array/#/someRight)_
  227. `some` starting from last element
  228. #### arr.splice(callback[, thisArg]) _(es5-ext/array/#/splice)_
  229. [_Updated with ECMAScript 6_](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-array.splice).
  230. ES6's version of `splice`, returns array of same type as the context.
  231. #### arr.uniq() _(es5-ext/array/#/uniq)_
  232. Returns duplicate-free version of the array
  233. #### arr.values() _(es5-ext/array/#/values)_
  234. [_Introduced with ECMAScript 6_](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-array.prototype.values).
  235. Returns iterator object which traverses all array values.
  236. #### arr[@@iterator] _(es5-ext/array/#/@@iterator)_
  237. [_Introduced with ECMAScript 6_](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-array.prototype-@@iterator).
  238. Returns iterator object which traverses all array values.
  239. ### Boolean Constructor extensions
  240. #### isBoolean(x) _(es5-ext/boolean/is-boolean)_
  241. Whether value is boolean
  242. ### Date Constructor extensions
  243. #### isDate(x) _(es5-ext/date/is-date)_
  244. Whether value is date instance
  245. #### validDate(x) _(es5-ext/date/valid-date)_
  246. If given object is not date throw TypeError in other case return it.
  247. ### Date Prototype extensions
  248. #### date.copy(date) _(es5-ext/date/#/copy)_
  249. Returns a copy of the date object
  250. #### date.daysInMonth() _(es5-ext/date/#/days-in-month)_
  251. Returns number of days of date's month
  252. #### date.floorDay() _(es5-ext/date/#/floor-day)_
  253. Sets the date time to 00:00:00.000
  254. #### date.floorMonth() _(es5-ext/date/#/floor-month)_
  255. Sets date day to 1 and date time to 00:00:00.000
  256. #### date.floorYear() _(es5-ext/date/#/floor-year)_
  257. Sets date month to 0, day to 1 and date time to 00:00:00.000
  258. #### date.format(pattern) _(es5-ext/date/#/format)_
  259. Formats date up to given string. Supported patterns:
  260. - `%Y` - Year with century, 1999, 2003
  261. - `%y` - Year without century, 99, 03
  262. - `%m` - Month, 01..12
  263. - `%d` - Day of the month 01..31
  264. - `%H` - Hour (24-hour clock), 00..23
  265. - `%M` - Minute, 00..59
  266. - `%S` - Second, 00..59
  267. - `%L` - Milliseconds, 000..999
  268. ### Error Constructor extensions
  269. #### custom(message/_, code, ext_/) _(es5-ext/error/custom)_
  270. Creates custom error object, optinally extended with `code` and other extension properties (provided with `ext` object)
  271. #### isError(x) _(es5-ext/error/is-error)_
  272. Whether value is an error (instance of `Error`).
  273. #### validError(x) _(es5-ext/error/valid-error)_
  274. If given object is not error throw TypeError in other case return it.
  275. ### Error Prototype extensions
  276. #### err.throw() _(es5-ext/error/#/throw)_
  277. Throws error
  278. ### Function Constructor extensions
  279. Some of the functions were inspired by [Functional JavaScript](http://osteele.com/sources/javascript/functional/) project by Olivier Steele
  280. #### constant(x) _(es5-ext/function/constant)_
  281. Returns a constant function that returns pregiven argument
  282. _k(x)(y) =def x_
  283. #### identity(x) _(es5-ext/function/identity)_
  284. Identity function. Returns first argument
  285. _i(x) =def x_
  286. #### invoke(name[, …args]) _(es5-ext/function/invoke)_
  287. Returns a function that takes an object as an argument, and applies object's
  288. _name_ method to arguments.
  289. _name_ can be name of the method or method itself.
  290. _invoke(name, …args)(object, …args2) =def object\[name\]\(…args, …args2\)_
  291. #### isArguments(x) _(es5-ext/function/is-arguments)_
  292. Whether value is arguments object
  293. #### isFunction(arg) _(es5-ext/function/is-function)_
  294. Whether value is instance of function
  295. #### noop() _(es5-ext/function/noop)_
  296. No operation function
  297. #### pluck(name) _(es5-ext/function/pluck)_
  298. Returns a function that takes an object, and returns the value of its _name_
  299. property
  300. _pluck(name)(obj) =def obj[name]_
  301. #### validFunction(arg) _(es5-ext/function/valid-function)_
  302. If given object is not function throw TypeError in other case return it.
  303. ### Function Prototype extensions
  304. Some of the methods were inspired by [Functional JavaScript](http://osteele.com/sources/javascript/functional/) project by Olivier Steele
  305. #### fn.compose([…fns]) _(es5-ext/function/#/compose)_
  306. Applies the functions in reverse argument-list order.
  307. _f1.compose(f2, f3, f4)(…args) =def f1(f2(f3(f4(…arg))))_
  308. `compose` can also be used in plain function form as:
  309. _compose(f1, f2, f3, f4)(…args) =def f1(f2(f3(f4(…arg))))_
  310. #### fn.copy() _(es5-ext/function/#/copy)_
  311. Produces copy of given function
  312. #### fn.curry([n]) _(es5-ext/function/#/curry)_
  313. Invoking the function returned by this function only _n_ arguments are passed to the underlying function. If the underlying function is not saturated, the result is a function that passes all its arguments to the underlying function.
  314. If _n_ is not provided then it defaults to context function length
  315. _f.curry(4)(arg1, arg2)(arg3)(arg4) =def f(arg1, args2, arg3, arg4)_
  316. #### fn.lock([…args]) _(es5-ext/function/#/lock)_
  317. Returns a function that applies the underlying function to _args_, and ignores its own arguments.
  318. _f.lock(…args)(…args2) =def f(…args)_
  319. _Named after it's counterpart in Google Closure_
  320. #### fn.not() _(es5-ext/function/#/not)_
  321. Returns a function that returns boolean negation of value returned by underlying function.
  322. _f.not()(…args) =def !f(…args)_
  323. #### fn.partial([…args]) _(es5-ext/function/#/partial)_
  324. Returns a function that when called will behave like context function called with initially passed arguments. If more arguments are suplilied, they are appended to initial args.
  325. _f.partial(…args1)(…args2) =def f(…args1, …args2)_
  326. #### fn.spread() _(es5-ext/function/#/spread)_
  327. Returns a function that applies underlying function with first list argument
  328. _f.match()(args) =def f.apply(null, args)_
  329. #### fn.toStringTokens() _(es5-ext/function/#/to-string-tokens)_
  330. Serializes function into two (arguments and body) string tokens. Result is plain object with `args` and `body` properties.
  331. ### Math extensions
  332. #### acosh(x) _(es5-ext/math/acosh)_
  333. [_Introduced with ECMAScript 6_](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-math.acosh).
  334. #### asinh(x) _(es5-ext/math/asinh)_
  335. [_Introduced with ECMAScript 6_](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-math.asinh).
  336. #### atanh(x) _(es5-ext/math/atanh)_
  337. [_Introduced with ECMAScript 6_](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-math.atanh).
  338. #### cbrt(x) _(es5-ext/math/cbrt)_
  339. [_Introduced with ECMAScript 6_](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-math.cbrt).
  340. #### clz32(x) _(es5-ext/math/clz32)_
  341. [_Introduced with ECMAScript 6_](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-math.clz32).
  342. #### cosh(x) _(es5-ext/math/cosh)_
  343. [_Introduced with ECMAScript 6_](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-math.cosh).
  344. #### expm1(x) _(es5-ext/math/expm1)_
  345. [_Introduced with ECMAScript 6_](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-math.expm1).
  346. #### fround(x) _(es5-ext/math/fround)_
  347. [_Introduced with ECMAScript 6_](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-math.fround).
  348. #### hypot([…values]) _(es5-ext/math/hypot)_
  349. [_Introduced with ECMAScript 6_](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-math.hypot).
  350. #### imul(x, y) _(es5-ext/math/imul)_
  351. [_Introduced with ECMAScript 6_](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-math.imul).
  352. #### log1p(x) _(es5-ext/math/log1p)_
  353. [_Introduced with ECMAScript 6_](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-math.log1p).
  354. #### log2(x) _(es5-ext/math/log2)_
  355. [_Introduced with ECMAScript 6_](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-math.log2).
  356. #### log10(x) _(es5-ext/math/log10)_
  357. [_Introduced with ECMAScript 6_](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-math.log10).
  358. #### sign(x) _(es5-ext/math/sign)_
  359. [_Introduced with ECMAScript 6_](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-math.sign).
  360. #### sinh(x) _(es5-ext/math/sinh)_
  361. [_Introduced with ECMAScript 6_](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-math.sinh).
  362. #### tanh(x) _(es5-ext/math/tanh)_
  363. [_Introduced with ECMAScript 6_](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-math.tanh).
  364. #### trunc(x) _(es5-ext/math/trunc)_
  365. [_Introduced with ECMAScript 6_](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-math.trunc).
  366. ### Number Constructor extensions
  367. #### EPSILON _(es5-ext/number/epsilon)_
  368. [_Introduced with ECMAScript 6_](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-number.epsilon).
  369. The difference between 1 and the smallest value greater than 1 that is representable as a Number value, which is approximately 2.2204460492503130808472633361816 x 10-16.
  370. #### isFinite(x) _(es5-ext/number/is-finite)_
  371. [_Introduced with ECMAScript 6_](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-number.isfinite).
  372. Whether value is finite. Differs from global isNaN that it doesn't do type coercion.
  373. #### isInteger(x) _(es5-ext/number/is-integer)_
  374. [_Introduced with ECMAScript 6_](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-number.isinteger).
  375. Whether value is integer.
  376. #### isNaN(x) _(es5-ext/number/is-nan)_
  377. [_Introduced with ECMAScript 6_](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-number.isnan).
  378. Whether value is NaN. Differs from global isNaN that it doesn't do type coercion.
  379. #### isNumber(x) _(es5-ext/number/is-number)_
  380. Whether given value is number
  381. #### isSafeInteger(x) _(es5-ext/number/is-safe-integer)_
  382. [_Introduced with ECMAScript 6_](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-number.issafeinteger).
  383. #### MAX*SAFE_INTEGER *(es5-ext/number/max-safe-integer)\_
  384. [_Introduced with ECMAScript 6_](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-number.maxsafeinteger).
  385. The value of Number.MAX_SAFE_INTEGER is 9007199254740991.
  386. #### MIN*SAFE_INTEGER *(es5-ext/number/min-safe-integer)\_
  387. [_Introduced with ECMAScript 6_](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-number.minsafeinteger).
  388. The value of Number.MIN_SAFE_INTEGER is -9007199254740991 (253-1).
  389. #### toInteger(x) _(es5-ext/number/to-integer)_
  390. Converts value to integer
  391. #### toPosInteger(x) _(es5-ext/number/to-pos-integer)_
  392. Converts value to positive integer. If provided value is less than 0, then 0 is returned
  393. #### toUint32(x) _(es5-ext/number/to-uint32)_
  394. Converts value to unsigned 32 bit integer. This type is used for array lengths.
  395. See: http://www.2ality.com/2012/02/js-integers.html
  396. ### Number Prototype extensions
  397. #### num.pad(length[, precision]) _(es5-ext/number/#/pad)_
  398. Pad given number with zeros. Returns string
  399. ### Object Constructor extensions
  400. #### assign(target, source[, …sourcen]) _(es5-ext/object/assign)_
  401. [_Introduced with ECMAScript 6_](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-object.assign).
  402. Extend _target_ by enumerable own properties of other objects. If properties are already set on target object, they will be overwritten.
  403. #### clear(obj) _(es5-ext/object/clear)_
  404. Remove all enumerable own properties of the object
  405. #### compact(obj) _(es5-ext/object/compact)_
  406. Returns copy of the object with all enumerable properties that have no falsy values
  407. #### compare(obj1, obj2) _(es5-ext/object/compare)_
  408. Universal cross-type compare function. To be used for e.g. array sort.
  409. #### copy(obj) _(es5-ext/object/copy)_
  410. Returns copy of the object with all enumerable properties.
  411. #### copyDeep(obj) _(es5-ext/object/copy-deep)_
  412. Returns deep copy of the object with all enumerable properties.
  413. #### count(obj) _(es5-ext/object/count)_
  414. Counts number of enumerable own properties on object
  415. #### create(obj[, properties]) _(es5-ext/object/create)_
  416. `Object.create` alternative that provides workaround for [V8 issue](http://code.google.com/p/v8/issues/detail?id=2804).
  417. When `null` is provided as a prototype, it's substituted with specially prepared object that derives from Object.prototype but has all Object.prototype properties shadowed with undefined.
  418. It's quirky solution that allows us to have plain objects with no truthy properties but with turnable prototype.
  419. Use only for objects that you plan to switch prototypes of and be aware of limitations of this workaround.
  420. #### eq(x, y) _(es5-ext/object/eq)_
  421. Whether two values are equal, using [_SameValueZero_](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-samevaluezero) algorithm.
  422. #### every(obj, cb[, thisArg[, compareFn]]) _(es5-ext/object/every)_
  423. Analogous to Array.prototype.every. Returns true if every key-value pair in this object satisfies the provided testing function.
  424. Optionally _compareFn_ can be provided which assures that keys are tested in given order. If provided _compareFn_ is equal to `true`, then order is alphabetical (by key).
  425. #### filter(obj, cb[, thisArg]) _(es5-ext/object/filter)_
  426. Analogous to Array.prototype.filter. Returns new object with properites for which _cb_ function returned truthy value.
  427. #### firstKey(obj) _(es5-ext/object/first-key)_
  428. Returns first enumerable key of the object, as keys are unordered by specification, it can be any key of an object.
  429. #### flatten(obj) _(es5-ext/object/flatten)_
  430. Returns new object, with flatten properties of input object
  431. _flatten({ a: { b: 1 }, c: { d: 1 } }) =def { b: 1, d: 1 }_
  432. #### forEach(obj, cb[, thisArg[, compareFn]]) _(es5-ext/object/for-each)_
  433. Analogous to Array.prototype.forEach. Calls a function for each key-value pair found in object
  434. Optionally _compareFn_ can be provided which assures that properties are iterated in given order. If provided _compareFn_ is equal to `true`, then order is alphabetical (by key).
  435. #### getPropertyNames() _(es5-ext/object/get-property-names)_
  436. Get all (not just own) property names of the object
  437. #### is(x, y) _(es5-ext/object/is)_
  438. Whether two values are equal, using [_SameValue_](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-samevaluezero) algorithm.
  439. #### isArrayLike(x) _(es5-ext/object/is-array-like)_
  440. Whether object is array-like object
  441. #### isCopy(x, y) _(es5-ext/object/is-copy)_
  442. Two values are considered a copy of same value when all of their own enumerable properties have same values.
  443. #### isCopyDeep(x, y) _(es5-ext/object/is-copy-deep)_
  444. Deep comparision of objects
  445. #### isEmpty(obj) _(es5-ext/object/is-empty)_
  446. True if object doesn't have any own enumerable property
  447. #### isObject(arg) _(es5-ext/object/is-object)_
  448. Whether value is not primitive
  449. #### isPlainObject(arg) _(es5-ext/object/is-plain-object)_
  450. Whether object is plain object, its protototype should be Object.prototype and it cannot be host object.
  451. #### keyOf(obj, searchValue) _(es5-ext/object/key-of)_
  452. Search object for value
  453. #### keys(obj) _(es5-ext/object/keys)_
  454. [_Updated with ECMAScript 6_](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-object.keys).
  455. ES6's version of `keys`, doesn't throw on primitive input
  456. #### map(obj, cb[, thisArg]) _(es5-ext/object/map)_
  457. Analogous to Array.prototype.map. Creates a new object with properties which values are results of calling a provided function on every key-value pair in this object.
  458. #### mapKeys(obj, cb[, thisArg]) _(es5-ext/object/map-keys)_
  459. Create new object with same values, but remapped keys
  460. #### mixin(target, source) _(es5-ext/object/mixin)_
  461. Extend _target_ by all own properties of other objects. Properties found in both objects will be overwritten (unless they're not configurable and cannot be overwritten).
  462. _It was for a moment part of ECMAScript 6 draft._
  463. #### mixinPrototypes(target, …source]) _(es5-ext/object/mixin-prototypes)_
  464. Extends _target_, with all source and source's prototype properties.
  465. Useful as an alternative for `setPrototypeOf` in environments in which it cannot be shimmed (no `__proto__` support).
  466. #### normalizeOptions(options) _(es5-ext/object/normalize-options)_
  467. Normalizes options object into flat plain object.
  468. Useful for functions in which we either need to keep options object for future reference or need to modify it for internal use.
  469. - It never returns input `options` object back (always a copy is created)
  470. - `options` can be undefined in such case empty plain object is returned.
  471. - Copies all enumerable properties found down prototype chain.
  472. #### primitiveSet([…names]) _(es5-ext/object/primitive-set)_
  473. Creates `null` prototype based plain object, and sets on it all property names provided in arguments to true.
  474. #### safeTraverse(obj[, …names]) _(es5-ext/object/safe-traverse)_
  475. Safe navigation of object properties. See http://wiki.ecmascript.org/doku.php?id=strawman:existential_operator
  476. #### serialize(value) _(es5-ext/object/serialize)_
  477. Serialize value into string. Differs from [JSON.stringify](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify) that it serializes also dates, functions and regular expresssions.
  478. #### setPrototypeOf(object, proto) _(es5-ext/object/set-prototype-of)_
  479. [_Introduced with ECMAScript 6_](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-object.setprototypeof).
  480. If native version is not provided, it depends on existence of `__proto__` functionality, if it's missing, `null` instead of function is exposed.
  481. #### some(obj, cb[, thisArg[, compareFn]]) _(es5-ext/object/some)_
  482. Analogous to Array.prototype.some Returns true if any key-value pair satisfies the provided
  483. testing function.
  484. Optionally _compareFn_ can be provided which assures that keys are tested in given order. If provided _compareFn_ is equal to `true`, then order is alphabetical (by key).
  485. #### toArray(obj[, cb[, thisArg[, compareFn]]]) _(es5-ext/object/to-array)_
  486. Creates an array of results of calling a provided function on every key-value pair in this object.
  487. Optionally _compareFn_ can be provided which assures that results are added in given order. If provided _compareFn_ is equal to `true`, then order is alphabetical (by key).
  488. #### unserialize(str) _(es5-ext/object/unserialize)_
  489. Userializes value previously serialized with [serialize](#serializevalue-es5-extobjectserialize)
  490. #### validCallable(x) _(es5-ext/object/valid-callable)_
  491. If given object is not callable throw TypeError in other case return it.
  492. #### validObject(x) _(es5-ext/object/valid-object)_
  493. Throws error if given value is not an object, otherwise it is returned.
  494. #### validValue(x) _(es5-ext/object/valid-value)_
  495. Throws error if given value is `null` or `undefined`, otherwise returns value.
  496. ### Promise Prototype extensions
  497. #### promise.finally(onFinally) _(es5-ext/promise/#/finally)_
  498. [_Introduced with ECMAScript 2018_](https://tc39.github.io/ecma262/#sec-promise.prototype.finally).
  499. ### RegExp Constructor extensions
  500. #### escape(str) _(es5-ext/reg-exp/escape)_
  501. Escapes string to be used in regular expression
  502. #### isRegExp(x) _(es5-ext/reg-exp/is-reg-exp)_
  503. Whether object is regular expression
  504. #### validRegExp(x) _(es5-ext/reg-exp/valid-reg-exp)_
  505. If object is regular expression it is returned, otherwise TypeError is thrown.
  506. ### RegExp Prototype extensions
  507. #### re.isSticky(x) _(es5-ext/reg-exp/#/is-sticky)_
  508. Whether regular expression has `sticky` flag.
  509. It's to be used as counterpart to [regExp.sticky](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-get-regexp.prototype.sticky) if it's not implemented.
  510. #### re.isUnicode(x) _(es5-ext/reg-exp/#/is-unicode)_
  511. Whether regular expression has `unicode` flag.
  512. It's to be used as counterpart to [regExp.unicode](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-get-regexp.prototype.unicode) if it's not implemented.
  513. #### re.match(string) _(es5-ext/reg-exp/#/match)_
  514. [_Introduced with ECMAScript 6_](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-regexp.prototype.match).
  515. #### re.replace(string, replaceValue) _(es5-ext/reg-exp/#/replace)_
  516. [_Introduced with ECMAScript 6_](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-regexp.prototype.replace).
  517. #### re.search(string) _(es5-ext/reg-exp/#/search)_
  518. [_Introduced with ECMAScript 6_](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-regexp.prototype.search).
  519. #### re.split(string) _(es5-ext/reg-exp/#/search)_
  520. [_Introduced with ECMAScript 6_](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-regexp.prototype.split).
  521. #### re.sticky _(es5-ext/reg-exp/#/sticky/implement)_
  522. [_Introduced with ECMAScript 6_](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-regexp.prototype.sticky).
  523. It's a getter, so only `implement` and `is-implemented` modules are provided.
  524. #### re.unicode _(es5-ext/reg-exp/#/unicode/implement)_
  525. [_Introduced with ECMAScript 6_](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-regexp.prototype.unicode).
  526. It's a getter, so only `implement` and `is-implemented` modules are provided.
  527. ### String Constructor extensions
  528. #### formatMethod(fMap) _(es5-ext/string/format-method)_
  529. Creates format method. It's used e.g. to create `Date.prototype.format` method
  530. #### fromCodePoint([…codePoints]) _(es5-ext/string/from-code-point)_
  531. [_Introduced with ECMAScript 6_](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-string.fromcodepoint)
  532. #### isString(x) _(es5-ext/string/is-string)_
  533. Whether object is string
  534. #### randomUniq() _(es5-ext/string/random-uniq)_
  535. Returns randomly generated id, with guarantee of local uniqueness (no same id will be returned twice)
  536. #### raw(callSite[, …substitutions]) _(es5-ext/string/raw)_
  537. [_Introduced with ECMAScript 6_](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-string.raw)
  538. ### String Prototype extensions
  539. #### str.at(pos) _(es5-ext/string/#/at)_
  540. _Proposed for ECMAScript 6/7 standard, but not (yet) in a draft_
  541. Returns a string at given position in Unicode-safe manner.
  542. Based on [implementation by Mathias Bynens](https://github.com/mathiasbynens/String.prototype.at).
  543. #### str.camelToHyphen() _(es5-ext/string/#/camel-to-hyphen)_
  544. Convert camelCase string to hyphen separated, e.g. one-two-three -> oneTwoThree.
  545. Useful when converting names from js property convention into filename convention.
  546. #### str.capitalize() _(es5-ext/string/#/capitalize)_
  547. Capitalize first character of a string
  548. #### str.caseInsensitiveCompare(str) _(es5-ext/string/#/case-insensitive-compare)_
  549. Case insensitive compare
  550. #### str.codePointAt(pos) _(es5-ext/string/#/code-point-at)_
  551. [_Introduced with ECMAScript 6_](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-string.prototype.codepointat)
  552. Based on [implementation by Mathias Bynens](https://github.com/mathiasbynens/String.prototype.codePointAt).
  553. #### str.contains(searchString[, position]) _(es5-ext/string/#/contains)_
  554. [_Introduced with ECMAScript 6_](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-string.prototype.contains)
  555. Whether string contains given string.
  556. #### str.endsWith(searchString[, endPosition]) _(es5-ext/string/#/ends-with)_
  557. [_Introduced with ECMAScript 6_](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-string.prototype.endswith).
  558. Whether strings ends with given string
  559. #### str.hyphenToCamel() _(es5-ext/string/#/hyphen-to-camel)_
  560. Convert hyphen separated string to camelCase, e.g. one-two-three -> oneTwoThree.
  561. Useful when converting names from filename convention to js property name convention.
  562. #### str.indent(str[, count]) _(es5-ext/string/#/indent)_
  563. Indents each line with provided _str_ (if _count_ given then _str_ is repeated _count_ times).
  564. #### str.last() _(es5-ext/string/#/last)_
  565. Return last character
  566. #### str.normalize([form]) _(es5-ext/string/#/normalize)_
  567. [_Introduced with ECMAScript 6_](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/normalize).
  568. Returns the Unicode Normalization Form of a given string.
  569. Based on Matsuza's version. Code used for integrated shim can be found at [github.com/walling/unorm](https://github.com/walling/unorm/blob/master/lib/unorm.js)
  570. #### str.pad(fill[, length]) _(es5-ext/string/#/pad)_
  571. Pad string with _fill_.
  572. If _length_ si given than _fill_ is reapated _length_ times.
  573. If _length_ is negative then pad is applied from right.
  574. #### str.repeat(n) _(es5-ext/string/#/repeat)_
  575. [_Introduced with ECMAScript 6_](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-string.prototype.repeat).
  576. Repeat given string _n_ times
  577. #### str.plainReplace(search, replace) _(es5-ext/string/#/plain-replace)_
  578. Simple `replace` version. Doesn't support regular expressions. Replaces just first occurrence of search string. Doesn't support insert patterns, therefore it is safe to replace text with text obtained programmatically (there's no need for additional _\$_ characters escape in such case).
  579. #### str.plainReplaceAll(search, replace) _(es5-ext/string/#/plain-replace-all)_
  580. Simple `replace` version. Doesn't support regular expressions. Replaces all occurrences of search string. Doesn't support insert patterns, therefore it is safe to replace text with text obtained programmatically (there's no need for additional _\$_ characters escape in such case).
  581. #### str.startsWith(searchString[, position]) _(es5-ext/string/#/starts-with)_
  582. [_Introduced with ECMAScript 6_](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-string.prototype.startswith).
  583. Whether strings starts with given string
  584. #### str[@@iterator] _(es5-ext/string/#/@@iterator)_
  585. [_Introduced with ECMAScript 6_](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-string.prototype-@@iterator).
  586. Returns iterator object which traverses all string characters (with respect to unicode symbols)
  587. ### Tests
  588. $ npm test
  589. ## Security contact information
  590. To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure.
  591. ## es5-ext for enterprise
  592. Available as part of the Tidelift Subscription
  593. The maintainers of es5-ext and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. [Learn more.](https://tidelift.com/subscription/pkg/npm-es5-ext?utm_source=npm-es5-ext&utm_medium=referral&utm_campaign=enterprise&utm_term=repo)
  594. [nix-build-image]: https://semaphoreci.com/api/v1/medikoo-org/es5-ext/branches/master/shields_badge.svg
  595. [nix-build-url]: https://semaphoreci.com/medikoo-org/es5-ext
  596. [win-build-image]: https://ci.appveyor.com/api/projects/status/3jox67ksw3p8hkwh/branch/master?svg=true
  597. [win-build-url]: https://ci.appveyor.com/project/medikoo/es5-ext
  598. [transpilation-image]: https://img.shields.io/badge/transpilation-free-brightgreen.svg
  599. [npm-image]: https://img.shields.io/npm/v/es5-ext.svg
  600. [npm-url]: https://www.npmjs.com/package/es5-ext