From 6bfdd921c1199e3e2e557d68e036bbe00d1c5523 Mon Sep 17 00:00:00 2001 From: reppners Date: Thu, 5 Mar 2015 11:19:07 +0100 Subject: [PATCH] + added missing typings to node path module including tests --- node/node-tests.ts | 140 +++++++++++++++++++++++++++++++++++++++++++++ node/node.d.ts | 13 +++++ 2 files changed, 153 insertions(+) diff --git a/node/node-tests.ts b/node/node-tests.ts index f2702ec4a..0ac9a9e8b 100644 --- a/node/node-tests.ts +++ b/node/node-tests.ts @@ -11,6 +11,7 @@ import http = require("http"); import net = require("net"); import dgram = require("dgram"); import querystring = require('querystring'); +import path = require("path"); assert(1 + 1 - 2 === 0, "The universe isn't how it should."); @@ -177,3 +178,142 @@ console.log(escaped); var unescaped: string = querystring.unescape(escaped); console.log(unescaped); // http://example.com/product/abcde.html + +//////////////////////////////////////////////////// +/// path tests : http://nodejs.org/api/path.html +//////////////////////////////////////////////////// + +module path_tests { + + path.normalize('/foo/bar//baz/asdf/quux/..'); + + path.join('/foo', 'bar', 'baz/asdf', 'quux', '..'); + // returns + //'/foo/bar/baz/asdf' + + try { + path.join('foo', {}, 'bar'); + } + catch(error) { + + } + + path.resolve('foo/bar', '/tmp/file/', '..', 'a/../subfile'); + //Is similar to: + // + //cd foo/bar + //cd /tmp/file/ + //cd .. + // cd a/../subfile + //pwd + + path.resolve('/foo/bar', './baz') + // returns + // '/foo/bar/baz' + + path.resolve('/foo/bar', '/tmp/file/') + // returns + // '/tmp/file' + + path.resolve('wwwroot', 'static_files/png/', '../gif/image.gif') + // if currently in /home/myself/node, it returns + // '/home/myself/node/wwwroot/static_files/gif/image.gif' + + path.isAbsolute('/foo/bar') // true + path.isAbsolute('/baz/..') // true + path.isAbsolute('qux/') // false + path.isAbsolute('.') // false + + path.isAbsolute('//server') // true + path.isAbsolute('C:/foo/..') // true + path.isAbsolute('bar\\baz') // false + path.isAbsolute('.') // false + + path.relative('C:\\orandea\\test\\aaa', 'C:\\orandea\\impl\\bbb') +// returns +// '..\\..\\impl\\bbb' + + path.relative('/data/orandea/test/aaa', '/data/orandea/impl/bbb') +// returns +// '../../impl/bbb' + + path.dirname('/foo/bar/baz/asdf/quux') +// returns +// '/foo/bar/baz/asdf' + + path.basename('/foo/bar/baz/asdf/quux.html') +// returns +// 'quux.html' + + path.basename('/foo/bar/baz/asdf/quux.html', '.html') +// returns +// 'quux' + + path.extname('index.html') +// returns +// '.html' + + path.extname('index.coffee.md') +// returns +// '.md' + + path.extname('index.') +// returns +// '.' + + path.extname('index') +// returns +// '' + + 'foo/bar/baz'.split(path.sep) +// returns +// ['foo', 'bar', 'baz'] + + 'foo\\bar\\baz'.split(path.sep) +// returns +// ['foo', 'bar', 'baz'] + + console.log(process.env.PATH) +// '/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin' + + process.env.PATH.split(path.delimiter) +// returns +// ['/usr/bin', '/bin', '/usr/sbin', '/sbin', '/usr/local/bin'] + + console.log(process.env.PATH) +// 'C:\Windows\system32;C:\Windows;C:\Program Files\nodejs\' + + process.env.PATH.split(path.delimiter) +// returns +// ['C:\Windows\system32', 'C:\Windows', 'C:\Program Files\nodejs\'] + + path.parse('/home/user/dir/file.txt') +// returns +// { +// root : "/", +// dir : "/home/user/dir", +// base : "file.txt", +// ext : ".txt", +// name : "file" +// } + + path.parse('C:\\path\\dir\\index.html') +// returns +// { +// root : "C:\", +// dir : "C:\path\dir", +// base : "index.html", +// ext : ".html", +// name : "index" +// } + + path.format({ + root : "/", + dir : "/home/user/dir", + base : "file.txt", + ext : ".txt", + name : "file" + }); +// returns +// '/home/user/dir/file.txt' +} diff --git a/node/node.d.ts b/node/node.d.ts index b1e849535..22fb332e8 100644 --- a/node/node.d.ts +++ b/node/node.d.ts @@ -953,14 +953,27 @@ declare module "fs" { } declare module "path" { + + export interface ParsedPath { + root: string; + dir: string; + base: string; + ext: string; + name: string; + } + export function normalize(p: string): string; export function join(...paths: any[]): string; export function resolve(...pathSegments: any[]): string; + export function isAbsolute(p: string): boolean; export function relative(from: string, to: string): string; export function dirname(p: string): string; export function basename(p: string, ext?: string): string; export function extname(p: string): string; export var sep: string; + export var delimiter: string; + export function parse(p: string): ParsedPath; + export function format(pP: ParsedPath): string; } declare module "string_decoder" {