This package provides a set of language helpers, utility functions, and classes for writing TypeScript applications. It includes APIs for feature detection, asynchronous and streaming operations, basic event handling, and making HTTP requests.
Using the latest Web technologies isn't always as straightforward as developers would like due to differing support
across platforms. dojo-core/has
provides a simple feature detection API that makes it easy to detect which platforms
support which features.
The default export of dojo-core/has
is a function which accepts a single parameter: the name of the feature to test for.
If the feature is available, a truthy value is returned, otherwise a falsy value is returned:
if (has('dom-addeventlistener')) {
element.addEventListener('click', function () { /* ... */ });
}
It's important to be able to add new feature tests that aren't provided out-of-the-box by dojo-core/has
.
This can be done easily by using the add
function exported by the has
module. It accepts two parameters:
the name of the feature, and either an immediate value indicating its availability or a function that resolves to a
value.
When a function is passed, the feature will be lazily evaluated - i.e. the function is not executed until the feature is actually requested. The return value is then cached for future calls for the same feature.
import { add as hasAdd } from 'dojo-core/has';
hasAdd('dom-queryselector', 'querySelector' in document && 'querySelectorAll' in document);
// Lazily executed; useful if a polyfill is loaded after page load
hasAdd('typedarray', function () {
return 'ArrayBuffer' in window;
});
dojo-core/has
maintains object hashes containing keys that correspond to all features that have been both
registered and requested. The value associated with each feature name key corresponds to that feature's availability
in the current environment. The object hash containing evaluated features is accessible via the cache
export.
The core package provides several modules offering a number of langauge utilities. Some of these are heavily based on methods in the ES2015 proposal; others are additional APIs for commonly-performed tasks.
The array
module contains analogues to the following ES2015 Array APIs:
copyWithin
- Copies a sequence of elements to another position in the given arrayfrom
- Creates anArray
from an array-like objectfill
- Fills some or all elements of an array with a given valuefind
- Returns the first value in the array satisfying a given functionfindIndex
- Returns the first index in the array whose value satisfies a given functionof
- Creates anArray
with the given arguments as its elements
The lang
module contains various utility functions for tasks such as copying objects and creating late-bound
or partial applications of functions.
The math
module contains analogues to a number of ES2015 APIs, including many trigonometric and logarithmic
functions.
The string
module contains analogues to the following ES2015 String APIs:
codePointAt
- Returns the UTF-16 encoded code point value of a position in a stringendsWith
- Determines whether a string ends with the given substringfromCodePoint
- Creates a string using the specified sequence of code pointsincludes
- Determines whether a string includes the given substringrepeat
- Returns a string containing a string repeated a given number of timesstartsWith
- Determines whether a string begins with the given substring
Special thanks to Mathias Bynens for granting permission to adopt code from his
codePointAt
,
fromCodePoint
, and
repeat
polyfills.
The string
module also contains the following utility functions:
escapeRegExp
- Escapes a string to safely be included in regular expressionsescapeXml
- Escapes XML (or HTML) content in a stringpadEnd
- Adds padding to the end of a string to ensure it is a certain lengthpadStart
- Adds padding to the beginning of a string to ensure it is a certain length
The UrlSearchParams
class can be used to parse and generate URL query strings.
The dojo-core/Promise
class is an implementation of the ES2015 Promise API that also includes static state inspection
and a finally
method for cleanup actions.
dojo-core/async
contains a number of classes and utility modules to simplify working with asynchronous operations.
The dojo-core/async/Task
class is an extension of dojo-core/Promise
that provides cancelation support.
Users will need to download and compile directly from the repository for the time being. Precompiled AMD/CommonJS modules will be provided in the near future as our release tools are improved.
We appreciate your interest! Please see the Guidelines Repository for the Contributing Guidelines and Style Guide.
Test cases MUST be written using Intern using the Object test interface and Assert assertion interface.
90% branch coverage MUST be provided for all code submitted to this repository, as reported by istanbul’s combined coverage results for all supported platforms.
© 2004–2015 Dojo Foundation & contributors. New BSD license.
Some string functions (codePointAt
, fromCodePoint
, and repeat
) adopted from polyfills by Mathias Bynens,
under the MIT license.
See LICENSE for details.