node_shims 0.2.1 copy "node_shims: ^0.2.1" to clipboard
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
copied to clipboard

Public API #

Path #

Usage: #

import "package:node_shims/path.dart" as path;
copied to clipboard

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

String normalize(String path)
copied to clipboard

Join all arguments together and normalize the resulting path.

String join(List paths)
copied to clipboard

Resolves to to an absolute path.

String resolve(List paths)
copied to clipboard

Solve the relative path from from to to.

String relative(String from, String to)
copied to clipboard

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

String dirname(String path)
copied to clipboard

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

String basename(String path, [String ext])
copied to clipboard

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

String extname(String path)
copied to clipboard

Check if it's an absolute path.

bool isAbsolute(String path)
copied to clipboard

Check if a file exists.

void exists(String path, void callback(bool))
bool existsSync (String path)
copied to clipboard

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

String sep;
copied to clipboard

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

String delimiter;
copied to clipboard

JS #

Usage: #

import "package:node_shims/js.dart";
copied to clipboard

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)
copied to clipboard

Return true if value is "falsey":

bool falsey(value) =>
  value == null || value == false || value == '' || value == 0 || value == double.NAN;

//Usage
if (falsey(''))
copied to clipboard

Return true if value is "truthy":

bool truthy(value) => !falsey(value);

//Usage
if (truthy(1))
copied to clipboard

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])
copied to clipboard

Returns a new array comprised of this array joined with other array(s) and/or value(s). docs.

List concat(List lists)
copied to clipboard

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

dynamic pop(List list)
copied to clipboard

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

int push(List list, item)
copied to clipboard

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

List reverse(List list)
copied to clipboard

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

dynamic shift(List list)
copied to clipboard

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)
copied to clipboard

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

List slice(List list, int begin, [int end])
copied to clipboard

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)));
copied to clipboard

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)));
copied to clipboard

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();
copied to clipboard

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])
copied to clipboard

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])
copied to clipboard

Strings #

Returns the character at the specified index. docs

String charAt(String str, int atPos) => str.substring(atPos, 1);
copied to clipboard

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

int charCodeAt(String str, int atPos) => str.codeUnitAt(atPos);
copied to clipboard

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

String quote(String str) => '"$str"';
copied to clipboard

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);
copied to clipboard

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

int search(String str, RegExp pattern) => str.indexOf(pattern);
copied to clipboard

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])
copied to clipboard

Trims whitespace from the left side of the string. docs

String trimLeft(String str) => str.replaceAll(new RegExp(r'^\s+'), '');
copied to clipboard

Trims whitespace from the right side of the string. docs

String trimRight(String str) => str.replaceAll(new RegExp(r'\s+$'), '');
copied to clipboard

HTML Encode html string.

String escapeHtml(String html)
copied to clipboard

Process #

Usage: #

import "pacakge:node_shims/process.dart" as process;
copied to clipboard

Returns the current working directory of the process.

String cwd()
copied to clipboard

An object containing the user environment.

Map<String,String> env
copied to clipboard

A Writable Stream to stdout.

IOSink get stdout
copied to clipboard

A writable stream to stderr.

IOSink get stderr
copied to clipboard

A Readable Stream for stdin.

Stream get stdin
copied to clipboard

An array containing the command line arguments.

List<String> get argv
copied to clipboard

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

String get execPath
copied to clipboard

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

void chdir(String directory)
copied to clipboard

Exit the Dart VM process immediately with the given code.

void exit([int code=0])
copied to clipboard

Utils #

Useful helper utils extracted from the 101 LINQ Samples.

Usage: #

import "package:node_shims/utils.dart";
copied to clipboard

Order a sequence by comparators or expressions. docs

order(List seq, {Comparator by, List<Comparator> byAll, on(x), List<Function> onAll})
copied to clipboard

A case-insensitive comparer that can be used in ordering and grouping functions.

caseInsensitiveComparer(a,b) => a.toUpperCase().compareTo(b.toUpperCase());
copied to clipboard

Group a sequance by comparators or expressions. docs

List<Group> group(Iterable seq, {by(x):null, Comparator matchWith:null, valuesAs(x):null})
copied to clipboard

Capture an expression and invoke it in the supplied function.

wrap(value, fn(x)) => fn(value);
copied to clipboard

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

String trimStart(String str, String start)
copied to clipboard

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

String trimEnd(String str, String end)
copied to clipboard

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

Contributors #

1
likes
10
points
45
downloads

Publisher

verified publishercheney-enterprises.com

Weekly Downloads

2024.09.19 - 2025.04.03

Common node and js utils to help in porting of node.js and javascript libs to dart.

Repository (GitHub)
View/report issues

License

unknown (license)

More

Packages that depend on node_shims