pwfree-es5-ext
v1.0.2
Published
ECMAScript extensions and shims
Downloads
1,021
Maintainers
Readme
Protestware free fork of es5-ext
ECMAScript 5 extensions
(with respect to ECMAScript 6 standard)
Shims for upcoming ES6 standard and other goodies implemented strictly with ECMAScript conventions in mind.
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.
When used in ECMAScript 6 environment, native implementation (if valid) takes precedence over shims.
Installation
npm i pwfree-pwfree-es5-ext
Usage
ECMAScript 6 features
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).
require("pwfree-es5-ext/array/from/implement");
Array.from("foo"); // ['f', 'o', 'o']
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.
var aFrom = require("pwfree-es5-ext/array/from");
aFrom("foo"); // ['f', 'o', 'o']
If you want to use shim unconditionally (even if native implementation exists) do:
var aFrom = require("pwfree-es5-ext/array/from/shim");
aFrom("foo"); // ['f', 'o', 'o']
List of ES6 shims
It's about properties introduced with ES6 and those that have been updated in new spec.
Array.from
->require('pwfree-es5-ext/array/from')
Array.of
->require('pwfree-es5-ext/array/of')
Array.prototype.concat
->require('pwfree-es5-ext/array/#/concat')
Array.prototype.copyWithin
->require('pwfree-es5-ext/array/#/copy-within')
Array.prototype.entries
->require('pwfree-es5-ext/array/#/entries')
Array.prototype.fill
->require('pwfree-es5-ext/array/#/fill')
Array.prototype.filter
->require('pwfree-es5-ext/array/#/filter')
Array.prototype.find
->require('pwfree-es5-ext/array/#/find')
Array.prototype.findIndex
->require('pwfree-es5-ext/array/#/find-index')
Array.prototype.keys
->require('pwfree-es5-ext/array/#/keys')
Array.prototype.map
->require('pwfree-es5-ext/array/#/map')
Array.prototype.slice
->require('pwfree-es5-ext/array/#/slice')
Array.prototype.splice
->require('pwfree-es5-ext/array/#/splice')
Array.prototype.values
->require('pwfree-es5-ext/array/#/values')
Array.prototype[@@iterator]
->require('pwfree-es5-ext/array/#/@@iterator')
Math.acosh
->require('pwfree-es5-ext/math/acosh')
Math.asinh
->require('pwfree-es5-ext/math/asinh')
Math.atanh
->require('pwfree-es5-ext/math/atanh')
Math.cbrt
->require('pwfree-es5-ext/math/cbrt')
Math.clz32
->require('pwfree-es5-ext/math/clz32')
Math.cosh
->require('pwfree-es5-ext/math/cosh')
Math.exmp1
->require('pwfree-es5-ext/math/expm1')
Math.fround
->require('pwfree-es5-ext/math/fround')
Math.hypot
->require('pwfree-es5-ext/math/hypot')
Math.imul
->require('pwfree-es5-ext/math/imul')
Math.log1p
->require('pwfree-es5-ext/math/log1p')
Math.log2
->require('pwfree-es5-ext/math/log2')
Math.log10
->require('pwfree-es5-ext/math/log10')
Math.sign
->require('pwfree-es5-ext/math/sign')
Math.signh
->require('pwfree-es5-ext/math/signh')
Math.tanh
->require('pwfree-es5-ext/math/tanh')
Math.trunc
->require('pwfree-es5-ext/math/trunc')
Number.EPSILON
->require('pwfree-es5-ext/number/epsilon')
Number.MAX_SAFE_INTEGER
->require('pwfree-es5-ext/number/max-safe-integer')
Number.MIN_SAFE_INTEGER
->require('pwfree-es5-ext/number/min-safe-integer')
Number.isFinite
->require('pwfree-es5-ext/number/is-finite')
Number.isInteger
->require('pwfree-es5-ext/number/is-integer')
Number.isNaN
->require('pwfree-es5-ext/number/is-nan')
Number.isSafeInteger
->require('pwfree-es5-ext/number/is-safe-integer')
Object.assign
->require('pwfree-es5-ext/object/assign')
Object.keys
->require('pwfree-es5-ext/object/keys')
Object.setPrototypeOf
->require('pwfree-es5-ext/object/set-prototype-of')
Promise.prototype.finally
->require('pwfree-es5-ext/promise/#/finally')
RegExp.prototype.match
->require('pwfree-es5-ext/reg-exp/#/match')
RegExp.prototype.replace
->require('pwfree-es5-ext/reg-exp/#/replace')
RegExp.prototype.search
->require('pwfree-es5-ext/reg-exp/#/search')
RegExp.prototype.split
->require('pwfree-es5-ext/reg-exp/#/split')
RegExp.prototype.sticky
-> Implement withrequire('pwfree-es5-ext/reg-exp/#/sticky/implement')
, use as function withrequire('pwfree-es5-ext/reg-exp/#/is-sticky')
RegExp.prototype.unicode
-> Implement withrequire('pwfree-es5-ext/reg-exp/#/unicode/implement')
, use as function withrequire('pwfree-es5-ext/reg-exp/#/is-unicode')
String.fromCodePoint
->require('pwfree-es5-ext/string/from-code-point')
String.raw
->require('pwfree-es5-ext/string/raw')
String.prototype.codePointAt
->require('pwfree-es5-ext/string/#/code-point-at')
String.prototype.contains
->require('pwfree-es5-ext/string/#/contains')
String.prototype.endsWith
->require('pwfree-es5-ext/string/#/ends-with')
String.prototype.normalize
->require('pwfree-es5-ext/string/#/normalize')
String.prototype.repeat
->require('pwfree-es5-ext/string/#/repeat')
String.prototype.startsWith
->require('pwfree-es5-ext/string/#/starts-with')
String.prototype[@@iterator]
->require('pwfree-es5-ext/string/#/@@iterator')
Non ECMAScript standard features
pwfree-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:
Object.defineProperty(Function.prototype, "partial", {
value: require("pwfree-es5-ext/function/#/partial"),
configurable: true,
enumerable: false,
writable: true,
});
Object.defineProperty(Array.prototype, "flatten", {
value: require("pwfree-es5-ext/array/#/flatten"),
configurable: true,
enumerable: false,
writable: true,
});
Object.defineProperty(String.prototype, "capitalize", {
value: require("pwfree-es5-ext/string/#/capitalize"),
configurable: true,
enumerable: false,
writable: true,
});
See pwfree-es5-extend, a great utility that automatically will extend natives for you.
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.
When you're in situation when native extensions are not good idea, then you should use methods indirectly:
var flatten = require("pwfree-es5-ext/array/#/flatten");
flatten.call([1, [2, [3, 4]]]); // [1, 2, 3, 4]
for better convenience you can turn methods into functions:
var call = Function.prototype.call;
var flatten = call.bind(require("pwfree-es5-ext/array/#/flatten"));
flatten([1, [2, [3, 4]]]); // [1, 2, 3, 4]
You can configure custom toolkit (like underscorejs), and use it throughout your application
var util = {};
util.partial = call.bind(require("pwfree-es5-ext/function/#/partial"));
util.flatten = call.bind(require("pwfree-es5-ext/array/#/flatten"));
util.startsWith = call.bind(require("pwfree-es5-ext/string/#/starts-with"));
util.flatten([1, [2, [3, 4]]]); // [1, 2, 3, 4]
As with native ones most methods are generic and can be run on any type of object.
API
Global extensions
global (pwfree-es5-ext/global)
Object that represents global scope
Array Constructor extensions
from(arrayLike[, mapFn[, thisArg]]) (pwfree-es5-ext/array/from)
Introduced with ECMAScript 6. Returns array representation of iterable or arrayLike. If arrayLike is an instance of array, its copy is returned.
generate([length[, …fill]]) (pwfree-es5-ext/array/generate)
Generate an array of pre-given length built of repeated arguments.
isPlainArray(x) (pwfree-es5-ext/array/is-plain-array)
Returns true if object is plain array (not instance of one of the Array's extensions).
of([…items]) (pwfree-es5-ext/array/of)
Introduced with ECMAScript 6. Create an array from given arguments.
toArray(obj) (pwfree-es5-ext/array/to-array)
Returns array representation of obj
. If obj
is already an array, obj
is returned back.
validArray(obj) (pwfree-es5-ext/array/valid-array)
Returns obj
if it's an array, otherwise throws TypeError
Array Prototype extensions
arr.binarySearch(compareFn) (pwfree-es5-ext/array/#/binary-search)
In sorted list search for index of item for which compareFn returns value closest to 0. It's variant of binary search algorithm
arr.clear() (pwfree-es5-ext/array/#/clear)
Clears the array
arr.compact() (pwfree-es5-ext/array/#/compact)
Returns a copy of the context with all non-values (null
or undefined
) removed.
arr.concat() (pwfree-es5-ext/array/#/concat)
Updated with ECMAScript 6.
ES6's version of concat
. Supports isConcatSpreadable
symbol, and returns array of same type as the context.
arr.contains(searchElement[, position]) (pwfree-es5-ext/array/#/contains)
Whether list contains the given value.
arr.copyWithin(target, start[, end]) (pwfree-es5-ext/array/#/copy-within)
arr.diff(other) (pwfree-es5-ext/array/#/diff)
Returns the array of elements that are present in context list but not present in other list.
arr.eIndexOf(searchElement[, fromIndex]) (pwfree-es5-ext/array/#/e-index-of)
egal version of indexOf
method. SameValueZero logic is used for comparision
arr.eLastIndexOf(searchElement[, fromIndex]) (pwfree-es5-ext/array/#/e-last-index-of)
egal version of lastIndexOf
method. SameValueZero logic is used for comparision
arr.entries() (pwfree-es5-ext/array/#/entries)
Introduced with ECMAScript 6. 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.
arr.exclusion([…lists]]) (pwfree-es5-ext/array/#/exclusion)
Returns the array of elements that are found only in one of the lists (either context list or list provided in arguments).
arr.fill(value[, start, end]) (pwfree-es5-ext/array/#/fill)
arr.filter(callback[, thisArg]) (pwfree-es5-ext/array/#/filter)
Updated with ECMAScript 6.
ES6's version of filter
, returns array of same type as the context.
arr.find(predicate[, thisArg]) (pwfree-es5-ext/array/#/find)
Introduced with ECMAScript 6. Return first element for which given function returns true
arr.findIndex(predicate[, thisArg]) (pwfree-es5-ext/array/#/find-index)
Introduced with ECMAScript 6. Return first index for which given function returns true
arr.first() (pwfree-es5-ext/array/#/first)
Returns value for first defined index
arr.firstIndex() (pwfree-es5-ext/array/#/first-index)
Returns first declared index of the array
arr.flatten() (pwfree-es5-ext/array/#/flatten)
Returns flattened version of the array
arr.forEachRight(cb[, thisArg]) (pwfree-es5-ext/array/#/for-each-right)
forEach
starting from last element
arr.group(cb[, thisArg]) (pwfree-es5-ext/array/#/group)
Group list elements by value returned by cb function
arr.indexesOf(searchElement[, fromIndex]) (pwfree-es5-ext/array/#/indexes-of)
Returns array of all indexes of given value
arr.intersection([…lists]) (pwfree-es5-ext/array/#/intersection)
Computes the array of values that are the intersection of all lists (context list and lists given in arguments)
arr.isCopy(other) (pwfree-es5-ext/array/#/is-copy)
Returns true if both context and other lists have same content
arr.isUniq() (pwfree-es5-ext/array/#/is-uniq)
Returns true if all values in array are unique
arr.keys() (pwfree-es5-ext/array/#/keys)
Introduced with ECMAScript 6. Returns iterator object, which traverses all array indexes.
arr.last() (pwfree-es5-ext/array/#/last)
Returns value of last defined index
arr.lastIndex() (pwfree-es5-ext/array/#/last)
Returns last defined index of the array
arr.map(callback[, thisArg]) (pwfree-es5-ext/array/#/map)
Updated with ECMAScript 6.
ES6's version of map
, returns array of same type as the context.
arr.remove(value[, …valuen]) (pwfree-es5-ext/array/#/remove)
Remove values from the array
arr.separate(sep) (pwfree-es5-ext/array/#/separate)
Returns array with items separated with sep
value
arr.slice(callback[, thisArg]) (pwfree-es5-ext/array/#/slice)
Updated with ECMAScript 6.
ES6's version of slice
, returns array of same type as the context.
arr.someRight(cb[, thisArg]) (pwfree-es5-ext/array/#/someRight)
some
starting from last element
arr.splice(callback[, thisArg]) (pwfree-es5-ext/array/#/splice)
Updated with ECMAScript 6.
ES6's version of splice
, returns array of same type as the context.
arr.uniq() (pwfree-es5-ext/array/#/uniq)
Returns duplicate-free version of the array
arr.values() (pwfree-es5-ext/array/#/values)
Introduced with ECMAScript 6. Returns iterator object which traverses all array values.
arr[@@iterator] (pwfree-es5-ext/array/#/@@iterator)
Introduced with ECMAScript 6. Returns iterator object which traverses all array values.
Boolean Constructor extensions
isBoolean(x) (pwfree-es5-ext/boolean/is-boolean)
Whether value is boolean
Date Constructor extensions
isDate(x) (pwfree-es5-ext/date/is-date)
Whether value is date instance
validDate(x) (pwfree-es5-ext/date/valid-date)
If given object is not date throw TypeError in other case return it.
Date Prototype extensions
date.copy(date) (pwfree-es5-ext/date/#/copy)
Returns a copy of the date object
date.daysInMonth() (pwfree-es5-ext/date/#/days-in-month)
Returns number of days of date's month
date.floorDay() (pwfree-es5-ext/date/#/floor-day)
Sets the date time to 00:00:00.000
date.floorMonth() (pwfree-es5-ext/date/#/floor-month)
Sets date day to 1 and date time to 00:00:00.000
date.floorYear() (pwfree-es5-ext/date/#/floor-year)
Sets date month to 0, day to 1 and date time to 00:00:00.000
date.format(pattern) (pwfree-es5-ext/date/#/format)
Formats date up to given string. Supported patterns:
%Y
- Year with century, 1999, 2003%y
- Year without century, 99, 03%m
- Month, 01..12%d
- Day of the month 01..31%H
- Hour (24-hour clock), 00..23%M
- Minute, 00..59%S
- Second, 00..59%L
- Milliseconds, 000..999
Error Constructor extensions
custom(message/, code, ext/) (pwfree-es5-ext/error/custom)
Creates custom error object, optinally extended with code
and other extension properties (provided with ext
object)
isError(x) (pwfree-es5-ext/error/is-error)
Whether value is an error (instance of Error
).
validError(x) (pwfree-es5-ext/error/valid-error)
If given object is not error throw TypeError in other case return it.
Error Prototype extensions
err.throw() (pwfree-es5-ext/error/#/throw)
Throws error
Function Constructor extensions
Some of the functions were inspired by Functional JavaScript project by Olivier Steele
constant(x) (pwfree-es5-ext/function/constant)
Returns a constant function that returns pregiven argument
k(x)(y) =def x
identity(x) (pwfree-es5-ext/function/identity)
Identity function. Returns first argument
i(x) =def x
invoke(name[, …args]) (pwfree-es5-ext/function/invoke)
Returns a function that takes an object as an argument, and applies object's name method to arguments. name can be name of the method or method itself.
invoke(name, …args)(object, …args2) =def object[name](…args, …args2)
isArguments(x) (pwfree-es5-ext/function/is-arguments)
Whether value is arguments object
isFunction(arg) (pwfree-es5-ext/function/is-function)
Whether value is instance of function
noop() (pwfree-es5-ext/function/noop)
No operation function
pluck(name) (pwfree-es5-ext/function/pluck)
Returns a function that takes an object, and returns the value of its name property
pluck(name)(obj) =def obj[name]
validFunction(arg) (pwfree-es5-ext/function/valid-function)
If given object is not function throw TypeError in other case return it.
Function Prototype extensions
Some of the methods were inspired by Functional JavaScript project by Olivier Steele
fn.compose([…fns]) (pwfree-es5-ext/function/#/compose)
Applies the functions in reverse argument-list order.
f1.compose(f2, f3, f4)(…args) =def f1(f2(f3(f4(…arg))))
compose
can also be used in plain function form as:
compose(f1, f2, f3, f4)(…args) =def f1(f2(f3(f4(…arg))))
fn.copy() (pwfree-es5-ext/function/#/copy)
Produces copy of given function
fn.curry([n]) (pwfree-es5-ext/function/#/curry)
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. If n is not provided then it defaults to context function length
f.curry(4)(arg1, arg2)(arg3)(arg4) =def f(arg1, args2, arg3, arg4)
fn.lock([…args]) (pwfree-es5-ext/function/#/lock)
Returns a function that applies the underlying function to args, and ignores its own arguments.
f.lock(…args)(…args2) =def f(…args)
Named after it's counterpart in Google Closure
fn.not() (pwfree-es5-ext/function/#/not)
Returns a function that returns boolean negation of value returned by underlying function.
f.not()(…args) =def !f(…args)
fn.partial([…args]) (pwfree-es5-ext/function/#/partial)
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.
f.partial(…args1)(…args2) =def f(…args1, …args2)
fn.spread() (pwfree-es5-ext/function/#/spread)
Returns a function that applies underlying function with first list argument
f.match()(args) =def f.apply(null, args)
fn.toStringTokens() (pwfree-es5-ext/function/#/to-string-tokens)
Serializes function into two (arguments and body) string tokens. Result is plain object with args
and body
properties.
Math extensions
acosh(x) (pwfree-es5-ext/math/acosh)
asinh(x) (pwfree-es5-ext/math/asinh)
atanh(x) (pwfree-es5-ext/math/atanh)
cbrt(x) (pwfree-es5-ext/math/cbrt)
clz32(x) (pwfree-es5-ext/math/clz32)
cosh(x) (pwfree-es5-ext/math/cosh)
expm1(x) (pwfree-es5-ext/math/expm1)
fround(x) (pwfree-es5-ext/math/fround)
hypot([…values]) (pwfree-es5-ext/math/hypot)
imul(x, y) (pwfree-es5-ext/math/imul)
log1p(x) (pwfree-es5-ext/math/log1p)
log2(x) (pwfree-es5-ext/math/log2)
log10(x) (pwfree-es5-ext/math/log10)
sign(x) (pwfree-es5-ext/math/sign)
sinh(x) (pwfree-es5-ext/math/sinh)
tanh(x) (pwfree-es5-ext/math/tanh)
trunc(x) (pwfree-es5-ext/math/trunc)
Number Constructor extensions
EPSILON (pwfree-es5-ext/number/epsilon)
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.
isFinite(x) (pwfree-es5-ext/number/is-finite)
Introduced with ECMAScript 6. Whether value is finite. Differs from global isNaN that it doesn't do type coercion.
isInteger(x) (pwfree-es5-ext/number/is-integer)
Introduced with ECMAScript 6. Whether value is integer.
isNaN(x) (pwfree-es5-ext/number/is-nan)
Introduced with ECMAScript 6. Whether value is NaN. Differs from global isNaN that it doesn't do type coercion.
isNumber(x) (pwfree-es5-ext/number/is-number)
Whether given value is number
isSafeInteger(x) (pwfree-es5-ext/number/is-safe-integer)
MAX*SAFE_INTEGER *(pwfree-es5-ext/number/max-safe-integer)_
Introduced with ECMAScript 6. The value of Number.MAX_SAFE_INTEGER is 9007199254740991.
MIN*SAFE_INTEGER *(pwfree-es5-ext/number/min-safe-integer)_
Introduced with ECMAScript 6. The value of Number.MIN_SAFE_INTEGER is -9007199254740991 (253-1).
toInteger(x) (pwfree-es5-ext/number/to-integer)
Converts value to integer
toPosInteger(x) (pwfree-es5-ext/number/to-pos-integer)
Converts value to positive integer. If provided value is less than 0, then 0 is returned
toUint32(x) (pwfree-es5-ext/number/to-uint32)
Converts value to unsigned 32 bit integer. This type is used for array lengths. See: http://www.2ality.com/2012/02/js-integers.html
Number Prototype extensions
num.pad(length[, precision]) (pwfree-es5-ext/number/#/pad)
Pad given number with zeros. Returns string
Object Constructor extensions
assign(target, source[, …sourcen]) (pwfree-es5-ext/object/assign)
Introduced with ECMAScript 6. Extend target by enumerable own properties of other objects. If properties are already set on target object, they will be overwritten.
clear(obj) (pwfree-es5-ext/object/clear)
Remove all enumerable own properties of the object
compact(obj) (pwfree-es5-ext/object/compact)
Returns copy of the object with all enumerable properties that have no falsy values
compare(obj1, obj2) (pwfree-es5-ext/object/compare)
Universal cross-type compare function. To be used for e.g. array sort.
copy(obj) (pwfree-es5-ext/object/copy)
Returns copy of the object with all enumerable properties.
copyDeep(obj) (pwfree-es5-ext/object/copy-deep)
Returns deep copy of the object with all enumerable properties.
count(obj) (pwfree-es5-ext/object/count)
Counts number of enumerable own properties on object
create(obj[, properties]) (pwfree-es5-ext/object/create)
Object.create
alternative that provides workaround for V8 issue.
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.
It's quirky solution that allows us to have plain objects with no truthy properties but with turnable prototype.
Use only for objects that you plan to switch prototypes of and be aware of limitations of this workaround.
eq(x, y) (pwfree-es5-ext/object/eq)
Whether two values are equal, using SameValueZero algorithm.
every(obj, cb[, thisArg[, compareFn]]) (pwfree-es5-ext/object/every)
Analogous to Array.prototype.every. Returns true if every key-value pair in this object satisfies the provided testing function.
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).
filter(obj, cb[, thisArg]) (pwfree-es5-ext/object/filter)
Analogous to Array.prototype.filter. Returns new object with properites for which cb function returned truthy value.
firstKey(obj) (pwfree-es5-ext/object/first-key)
Returns first enumerable key of the object, as keys are unordered by specification, it can be any key of an object.
flatten(obj) (pwfree-es5-ext/object/flatten)
Returns new object, with flatten properties of input object
flatten({ a: { b: 1 }, c: { d: 1 } }) =def { b: 1, d: 1 }
forEach(obj, cb[, thisArg[, compareFn]]) (pwfree-es5-ext/object/for-each)
Analogous to Array.prototype.forEach. Calls a function for each key-value pair found in object
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).
getPropertyNames() (pwfree-es5-ext/object/get-property-names)
Get all (not just own) property names of the object
is(x, y) (pwfree-es5-ext/object/is)
Whether two values are equal, using SameValue algorithm.
isArrayLike(x) (pwfree-es5-ext/object/is-array-like)
Whether object is array-like object
isCopy(x, y) (pwfree-es5-ext/object/is-copy)
Two values are considered a copy of same value when all of their own enumerable properties have same values.
isCopyDeep(x, y) (pwfree-es5-ext/object/is-copy-deep)
Deep comparision of objects
isEmpty(obj) (pwfree-es5-ext/object/is-empty)
True if object doesn't have any own enumerable property
isObject(arg) (pwfree-es5-ext/object/is-object)
Whether value is not primitive
isPlainObject(arg) (pwfree-es5-ext/object/is-plain-object)
Whether object is plain object, its protototype should be Object.prototype and it cannot be host object.
keyOf(obj, searchValue) (pwfree-es5-ext/object/key-of)
Search object for value
keys(obj) (pwfree-es5-ext/object/keys)
Updated with ECMAScript 6.
ES6's version of keys
, doesn't throw on primitive input
map(obj, cb[, thisArg]) (pwfree-es5-ext/object/map)
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.
mapKeys(obj, cb[, thisArg]) (pwfree-es5-ext/object/map-keys)
Create new object with same values, but remapped keys
mixin(target, source) (pwfree-es5-ext/object/mixin)
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). It was for a moment part of ECMAScript 6 draft.
mixinPrototypes(target, …source]) (pwfree-es5-ext/object/mixin-prototypes)
Extends target, with all source and source's prototype properties.
Useful as an alternative for setPrototypeOf
in environments in which it cannot be shimmed (no __proto__
support).
normalizeOptions(options) (pwfree-es5-ext/object/normalize-options)
Normalizes options object into flat plain object.
Useful for functions in which we either need to keep options object for future reference or need to modify it for internal use.
- It never returns input
options
object back (always a copy is created) options
can be undefined in such case empty plain object is returned.- Copies all enumerable properties found down prototype chain.
primitiveSet([…names]) (pwfree-es5-ext/object/primitive-set)
Creates null
prototype based plain object, and sets on it all property names provided in arguments to true.
safeTraverse(obj[, …names]) (pwfree-es5-ext/object/safe-traverse)
Safe navigation of object properties. See http://wiki.ecmascript.org/doku.php?id=strawman:existential_operator
serialize(value) (pwfree-es5-ext/object/serialize)
Serialize value into string. Differs from JSON.stringify that it serializes also dates, functions and regular expresssions.
setPrototypeOf(object, proto) (pwfree-es5-ext/object/set-prototype-of)
Introduced with ECMAScript 6.
If native version is not provided, it depends on existence of __proto__
functionality, if it's missing, null
instead of function is exposed.
some(obj, cb[, thisArg[, compareFn]]) (pwfree-es5-ext/object/some)
Analogous to Array.prototype.some Returns true if any key-value pair satisfies the provided
testing function.
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).
toArray(obj[, cb[, thisArg[, compareFn]]]) (pwfree-es5-ext/object/to-array)
Creates an array of results of calling a provided function on every key-value pair in this object.
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).
unserialize(str) (pwfree-es5-ext/object/unserialize)
Userializes value previously serialized with serialize
validCallable(x) (pwfree-es5-ext/object/valid-callable)
If given object is not callable throw TypeError in other case return it.
validObject(x) (pwfree-es5-ext/object/valid-object)
Throws error if given value is not an object, otherwise it is returned.
validValue(x) (pwfree-es5-ext/object/valid-value)
Throws error if given value is null
or undefined
, otherwise returns value.
Promise Prototype extensions
promise.finally(onFinally) (pwfree-es5-ext/promise/#/finally)
Introduced with ECMAScript 2018.
RegExp Constructor extensions
escape(str) (pwfree-es5-ext/reg-exp/escape)
Escapes string to be used in regular expression
isRegExp(x) (pwfree-es5-ext/reg-exp/is-reg-exp)
Whether object is regular expression
validRegExp(x) (pwfree-es5-ext/reg-exp/valid-reg-exp)
If object is regular expression it is returned, otherwise TypeError is thrown.
RegExp Prototype extensions
re.isSticky(x) (pwfree-es5-ext/reg-exp/#/is-sticky)
Whether regular expression has sticky
flag.
It's to be used as counterpart to regExp.sticky if it's not implemented.
re.isUnicode(x) (pwfree-es5-ext/reg-exp/#/is-unicode)
Whether regular expression has unicode
flag.
It's to be used as counterpart to regExp.unicode if it's not implemented.
re.match(string) (pwfree-es5-ext/reg-exp/#/match)
re.replace(string, replaceValue) (pwfree-es5-ext/reg-exp/#/replace)
re.search(string) (pwfree-es5-ext/reg-exp/#/search)
re.split(string) (pwfree-es5-ext/reg-exp/#/search)
re.sticky (pwfree-es5-ext/reg-exp/#/sticky/implement)
Introduced with ECMAScript 6.
It's a getter, so only implement
and is-implemented
modules are provided.
re.unicode (pwfree-es5-ext/reg-exp/#/unicode/implement)
Introduced with ECMAScript 6.
It's a getter, so only implement
and is-implemented
modules are provided.
String Constructor extensions
formatMethod(fMap) (pwfree-es5-ext/string/format-method)
Creates format method. It's used e.g. to create Date.prototype.format
method
fromCodePoint([…codePoints]) (pwfree-es5-ext/string/from-code-point)
isString(x) (pwfree-es5-ext/string/is-string)
Whether object is string
randomUniq() (pwfree-es5-ext/string/random-uniq)
Returns randomly generated id, with guarantee of local uniqueness (no same id will be returned twice)
raw(callSite[, …substitutions]) (pwfree-es5-ext/string/raw)
String Prototype extensions
str.at(pos) (pwfree-es5-ext/string/#/at)
Proposed for ECMAScript 6/7 standard, but not (yet) in a draft
Returns a string at given position in Unicode-safe manner. Based on implementation by Mathias Bynens.
str.camelToHyphen() (pwfree-es5-ext/string/#/camel-to-hyphen)
Convert camelCase string to hyphen separated, e.g. one-two-three -> oneTwoThree. Useful when converting names from js property convention into filename convention.
str.capitalize() (pwfree-es5-ext/string/#/capitalize)
Capitalize first character of a string
str.caseInsensitiveCompare(str) (pwfree-es5-ext/string/#/case-insensitive-compare)
Case insensitive compare
str.codePointAt(pos) (pwfree-es5-ext/string/#/code-point-at)
Based on implementation by Mathias Bynens.
str.contains(searchString[, position]) (pwfree-es5-ext/string/#/contains)
Whether string contains given string.
str.endsWith(searchString[, endPosition]) (pwfree-es5-ext/string/#/ends-with)
Introduced with ECMAScript 6. Whether strings ends with given string
str.hyphenToCamel() (pwfree-es5-ext/string/#/hyphen-to-camel)
Convert hyphen separated string to camelCase, e.g. one-two-three -> oneTwoThree. Useful when converting names from filename convention to js property name convention.
str.indent(str[, count]) (pwfree-es5-ext/string/#/indent)
Indents each line with provided str (if count given then str is repeated count times).
str.last() (pwfree-es5-ext/string/#/last)
Return last character
str.normalize([form]) (pwfree-es5-ext/string/#/normalize)
Introduced with ECMAScript 6. Returns the Unicode Normalization Form of a given string. Based on Matsuza's version. Code used for integrated shim can be found at github.com/walling/unorm
str.pad(fill[, length]) (pwfree-es5-ext/string/#/pad)
Pad string with fill. If length si given than fill is reapated length times. If length is negative then pad is applied from right.
str.repeat(n) (pwfree-es5-ext/string/#/repeat)
Introduced with ECMAScript 6. Repeat given string n times
str.plainReplace(search, replace) (pwfree-es5-ext/string/#/plain-replace)
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).
str.plainReplaceAll(search, replace) (pwfree-es5-ext/string/#/plain-replace-all)
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).
str.startsWith(searchString[, position]) (pwfree-es5-ext/string/#/starts-with)
Introduced with ECMAScript 6. Whether strings starts with given string
str[@@iterator] (pwfree-es5-ext/string/#/@@iterator)
Introduced with ECMAScript 6. Returns iterator object which traverses all string characters (with respect to unicode symbols)
Tests
$ npm test