node_shims 0.2.1 node_shims: ^0.2.1 copied to clipboard
Common node and js utils to help in porting of node.js and javascript libs to dart.
Node and JavaScript API Shims #
Common node and js utils to help in porting of node.js and javascript libs to dart. Behavior of libs should match the js implementation as closely as dart allows.
Installing via Pub #
Add this to your package's pubspec.yaml file:
dependencies:
node_shims: 0.1.3
Public API #
Path #
Usage: #
import "package:node_shims/path.dart" as path;
Normalize a string path, taking care of '..' and '.' parts.
String normalize(String path)
Join all arguments together and normalize the resulting path.
String join(List paths)
Resolves to
to an absolute path.
String resolve(List paths)
Solve the relative path from from
to to
.
String relative(String from, String to)
Return the directory name of a path. Similar to the Unix dirname
command.
String dirname(String path)
Return the last portion of a path. Similar to the Unix basename
command.
String basename(String path, [String ext])
Return the extension of the path, from the last '.' to end of string in the last portion of the path.
String extname(String path)
Check if it's an absolute path.
bool isAbsolute(String path)
Check if a file exists.
void exists(String path, void callback(bool))
bool existsSync (String path)
The platform-specific file separator. '\' or '/'.
String sep;
The platform-specific path delimiter, ; or ':'.
String delimiter;
JS #
Usage: #
import "package:node_shims/js.dart";
Core JS functions #
If value is truthy return value, otherwise return defaultValue. If defaultValue is a function it's result is returned.
or(value, defaultValue)
//js
value || defaultValue
//Usage
or(null, 1)
or(null, () => 1)
Return true if value
is "falsey":
bool falsey(value) =>
value == null || value == false || value == '' || value == 0 || value == double.NAN;
//Usage
if (falsey(''))
Return true if value
is "truthy":
bool truthy(value) => !falsey(value);
//Usage
if (truthy(1))
Array functions #
Changes the content of an array, adding new elements while removing old elements. docs.
List splice(List list, int index, [num howMany=0, dynamic elements])
Returns a new array comprised of this array joined with other array(s) and/or value(s). docs.
List concat(List lists)
Removes the last element from an array and returns that element. docs
dynamic pop(List list)
Mutates an array by appending the given elements and returning the new length of the array. docs
int push(List list, item)
Reverses an array in place. The first array element becomes the last and the last becomes the first. docs
List reverse(List list)
Removes the first element from an array and returns that element. This method changes the length of the array. docs
dynamic shift(List list)
Adds one or more elements to the beginning of an array and returns the new length of the array. docs
int unshift(List list, item)
Returns a shallow copy of a portion of an array. docs
List slice(List list, int begin, [int end])
Tests whether all elements in the array passes (truthy) the test implemented by the provided function. docs
bool every(List list, fn(e)) => list.every((x) => truthy(fn(x)));
Tests whether some element in the array passes (truthy) the test implemented by the provided function. docs
bool some(List list, fn(e)) => list.any((x) => truthy(fn(x)));
Creates a new array with all elements that pass the test implemented by the provided function. docs
List filter(List list, fn(e)) => list.where((x) => truthy(fn(x))).toList();
Apply a function against an accumulator and each value of the array (from left-to-right) as to reduce it to a single value. docs
dynamic reduce(List list, fn(prev, curr, int index, List list), [initialValue])
Apply a function simultaneously against two values of the array (from right-to-left) as to reduce it to a single value. docs
dynamic reduceRight(List list, fn(prev, curr, int index, List list), [initialValue])
Strings #
Returns the character at the specified index. docs
String charAt(String str, int atPos) => str.substring(atPos, 1);
Returns a number indicating the Unicode value of the character at the given index. docs
int charCodeAt(String str, int atPos) => str.codeUnitAt(atPos);
Wraps the string in double quotes ("""). docs
String quote(String str) => '"$str"';
Used to find a match between a regular expression and a string, and to replace the matched substring with a new substring. docs
String replace(String str, pattern) => str.replaceAll(str, pattern);
Executes the search for a match between a regular expression and a specified string. docs
int search(String str, RegExp pattern) => str.indexOf(pattern);
Returns the characters in a string beginning at the specified location through the specified number of characters. docs
String substr(String str, int start, [int length=null])
Trims whitespace from the left side of the string. docs
String trimLeft(String str) => str.replaceAll(new RegExp(r'^\s+'), '');
Trims whitespace from the right side of the string. docs
String trimRight(String str) => str.replaceAll(new RegExp(r'\s+$'), '');
HTML Encode html
string.
String escapeHtml(String html)
Process #
Usage: #
import "pacakge:node_shims/process.dart" as process;
Returns the current working directory of the process.
String cwd()
An object containing the user environment.
Map<String,String> env
A Writable Stream to stdout.
IOSink get stdout
A writable stream to stderr.
IOSink get stderr
A Readable Stream for stdin.
Stream get stdin
An array containing the command line arguments.
List<String> get argv
This is the absolute pathname of the executable that started the process.
String get execPath
Changes the current working directory of the process or throws an exception if that fails.
void chdir(String directory)
Exit the Dart VM process immediately with the given code
.
void exit([int code=0])
Utils #
Useful helper utils extracted from the 101 LINQ Samples.
Usage: #
import "package:node_shims/utils.dart";
Order a sequence by comparators or expressions. docs
order(List seq, {Comparator by, List<Comparator> byAll, on(x), List<Function> onAll})
A case-insensitive comparer that can be used in ordering and grouping functions.
caseInsensitiveComparer(a,b) => a.toUpperCase().compareTo(b.toUpperCase());
Group a sequance by comparators or expressions. docs
List<Group> group(Iterable seq, {by(x):null, Comparator matchWith:null, valuesAs(x):null})
Capture an expression and invoke it in the supplied function.
wrap(value, fn(x)) => fn(value);
Trim the start of a string if it matches the specified string.
String trimStart(String str, String start)
Trim the end of a string if it matches the specified string.
String trimEnd(String str, String end)
Pull requests for missing js or node.js utils welcome.
Contributors #
- mythz (Demis Bellot)
- MaxHorstmann (Max Horstmann)
- cheney-enterprises (Eric Cheney)