Skip to content

Repository files navigation

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.

Add this to your package's pubspec.yaml file:

dependencies:
node_shims: 0.1.3

Public API

Usage:

import"package:node_shims/path.dart"as path;

Normalize a string path, taking care of '..' and '.' parts.

Stringnormalize(String path)

Join all arguments together and normalize the resulting path.

Stringjoin(List paths)

Resolves to to an absolute path.

Stringresolve(List paths)

Solve the relative path from from to to.

Stringrelative(String from, String to)

Return the directory name of a path. Similar to the Unix dirname command.

Stringdirname(String path)

Return the last portion of a path. Similar to the Unix basename command.

Stringbasename(String path, [String ext])

Return the extension of the path, from the last '.' to end of string in the last portion of the path.

Stringextname(String path)

Check if it's an absolute path.

boolisAbsolute(String path)

Check if a file exists.

voidexists(String path, voidcallback(bool))
bool existsSync (String path)

The platform-specific file separator. '\' or '/'.

String sep;

The platform-specific path delimiter, ; or ':'.

String delimiter;

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
//Usageor(null, 1)
or(null, () =>1)

Return true if value is "falsey":

boolfalsey(value) =>
value ==null|| value ==false|| value ==''|| value ==0|| value ==double.NAN;
//Usageif (falsey(''))

Return true if value is "truthy":

booltruthy(value) =>!falsey(value);
//Usageif (truthy(1))

Array functions

Changes the content of an array, adding new elements while removing old elements. docs.

Listsplice(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.

Listconcat(List lists)

Removes the last element from an array and returns that element. docs

dynamicpop(List list)

Mutates an array by appending the given elements and returning the new length of the array. docs

intpush(List list, item)

Reverses an array in place. The first array element becomes the last and the last becomes the first. docs

Listreverse(List list)

Removes the first element from an array and returns that element. This method changes the length of the array. docs

dynamicshift(List list)

Adds one or more elements to the beginning of an array and returns the new length of the array. docs

intunshift(List list, item)

Returns a shallow copy of a portion of an array. docs

Listslice(List list, int begin, [int end])

Tests whether all elements in the array passes (truthy) the test implemented by the provided function. docs

boolevery(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

boolsome(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

Listfilter(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

dynamicreduce(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

dynamicreduceRight(List list, fn(prev, curr, int index, List list), [initialValue])

Strings

Returns the character at the specified index. docs

StringcharAt(String str, int atPos) => str.substring(atPos, 1);

Returns a number indicating the Unicode value of the character at the given index. docs

intcharCodeAt(String str, int atPos) => str.codeUnitAt(atPos);

Wraps the string in double quotes ("""). docs

Stringquote(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

Stringreplace(String str, pattern) => str.replaceAll(str, pattern);

Executes the search for a match between a regular expression and a specified string. docs

intsearch(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

Stringsubstr(String str, int start, [int length=null])

Trims whitespace from the left side of the string. docs

StringtrimLeft(String str) => str.replaceAll(newRegExp(r'^\s+'), '');

Trims whitespace from the right side of the string. docs

StringtrimRight(String str) => str.replaceAll(newRegExp(r'\s+$'), '');

HTML Encode html string.

StringescapeHtml(String html)

Usage:

import"pacakge:node_shims/process.dart"as process;

Returns the current working directory of the process.

Stringcwd()

An object containing the user environment.

Map<String,String> env

A Writable Stream to stdout.

IOSinkget stdout

A writable stream to stderr.

IOSinkget stderr

A Readable Stream for stdin.

Streamget stdin

An array containing the command line arguments.

List<String> get argv

This is the absolute pathname of the executable that started the process.

Stringget execPath

Changes the current working directory of the process or throws an exception if that fails.

voidchdir(String directory)

Exit the Dart VM process immediately with the given code.

voidexit([int code=0])

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.

StringtrimStart(String str, String start)

Trim the end of a string if it matches the specified string.

StringtrimEnd(String str, String end)

Pull requests for missing js or node.js utils welcome.

Contributors

About

Node API shims to help with porting server apps to Dart

Resources

Stars

5 stars

Watchers

3 watching

Forks

Releases

Packages

Contributors

Languages