Skip to content

Commit 4d0e28d

Browse files
committed
Initial commit
0 parents  commit 4d0e28d

File tree

7 files changed

+231
-0
lines changed

7 files changed

+231
-0
lines changed

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/js/
2+
/externs/
3+
/node_modules/
4+
/bower_components/
5+
/tmp/

Gruntfile.js

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
module.exports = function(grunt) {
2+
3+
"use strict";
4+
5+
grunt.initConfig({
6+
7+
libFiles: [
8+
"src/**/*.purs",
9+
"bower_components/purescript-*/src/**/*.purs",
10+
"bower_components/purescript-*/src/**/*.purs.hs"
11+
],
12+
13+
clean: {
14+
tests: ["tmp"],
15+
lib: ["js", "externs"]
16+
},
17+
18+
"purescript-make": {
19+
options: {
20+
tco: true,
21+
magicDo: true
22+
},
23+
lib: {
24+
src: "<%=libFiles%>"
25+
}
26+
},
27+
28+
purescript: {
29+
options: {
30+
tco: true,
31+
magicDo: true
32+
},
33+
tests: {
34+
options: {
35+
main: "SimplePathTests",
36+
module: ["SimplePathTests"]
37+
},
38+
src: ["tests/Simple.purs", "<%=libFiles%>"],
39+
dest: "tmp/tests.js"
40+
}
41+
},
42+
43+
execute: {
44+
tests: {
45+
src: "tmp/tests.js"
46+
}
47+
}
48+
49+
});
50+
51+
grunt.loadNpmTasks("grunt-contrib-clean");
52+
grunt.loadNpmTasks("grunt-purescript");
53+
grunt.loadNpmTasks("grunt-execute");
54+
55+
grunt.registerTask("test", ["clean:tests", "purescript:tests", "execute:tests"]);
56+
grunt.registerTask("lib", ["purescript-make:lib"]);
57+
grunt.registerTask("default", ["test", "lib"]);
58+
};

LICENSE

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2014 PureScript
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software is furnished to do so,
10+
subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

bower.json

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "purescript-node-path",
3+
"version": "0.0.0",
4+
"description": "Type declarations for Node's Path module",
5+
"keywords": [
6+
"purescript"
7+
],
8+
"license": "MIT",
9+
"ignore": [
10+
"**/.*",
11+
"node_modules",
12+
"bower_components",
13+
"externs",
14+
"js",
15+
"tmp"
16+
],
17+
"devDependencies": {
18+
"purescript-exceptions": "*"
19+
}
20+
}

package.json

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "purescript-node-path",
3+
"version": "0.0.0",
4+
"devDependencies": {
5+
"grunt": "~0.4.4",
6+
"grunt-purescript": "~0.3.1",
7+
"grunt-contrib-clean": "~0.5.0",
8+
"grunt-execute": "~0.1.5"
9+
}
10+
}

src/Node/Path.purs

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
module Node.Path where
2+
3+
-- |
4+
-- Type for strings representing file paths.
5+
--
6+
type FilePath = String
7+
8+
-- |
9+
-- Normalize a string path, taking care of `..` and `.`, duplicated slashes,
10+
-- etc. If the path contains a trailing slash it is preserved. On Windows
11+
-- backslashes are used.
12+
--
13+
foreign import normalize
14+
"var normalize = require('path').normalize;" :: FilePath -> FilePath
15+
16+
-- |
17+
-- Joins two path segments together and normalizes the resulting path.
18+
--
19+
foreign import join
20+
"var join = function (start) { \
21+
\ return function (end) { \
22+
\ return require('path').join(start, end); \
23+
\ }; \
24+
\}" :: FilePath -> FilePath -> FilePath
25+
26+
-- |
27+
-- Resolves `to` to an absolute path ([from...], to).
28+
--
29+
foreign import resolve
30+
"var resolve = function (from) { \
31+
\ return function (to) { \
32+
\ return require('path').resolve.apply(this, from.concat([to])); \
33+
\ }; \
34+
\};" :: [FilePath] -> FilePath -> FilePath
35+
36+
-- |
37+
-- Solve the relative path from `from` to `to`.
38+
--
39+
foreign import relative
40+
"var relative = function (from) { \
41+
\ return function (to) { \
42+
\ return require('path').relative(from, to); \
43+
\ }; \
44+
\}" :: FilePath -> FilePath -> FilePath
45+
46+
-- |
47+
-- Return the directory name of a path.
48+
--
49+
foreign import dirname
50+
"var dirname = function (path) { \
51+
\ var p = require('path'); \
52+
\ return p.normalize(p.dirname(path)); \
53+
\}" :: FilePath -> FilePath
54+
55+
-- |
56+
-- Return the last portion of a path.
57+
--
58+
foreign import basename
59+
"var basename = require('path').basename;" :: FilePath -> FilePath
60+
61+
-- |
62+
-- Return the last portion of a path, also dropping a specific file extension
63+
-- if it matches the end of the name.
64+
--
65+
foreign import basenameWithoutExt
66+
"var basenameWithoutExt = function (path) { \
67+
\ return function (ext) { \
68+
\ return require('path').basename(path, ext); \
69+
\ }; \
70+
\}" :: FilePath -> FilePath -> FilePath
71+
72+
-- |
73+
-- Return the extension of the path, from the last `.` to end of string in the
74+
-- last portion of the path. If there is no `.` in the last portion of the path
75+
-- or the first character of it is `.`, then it returns an empty string.
76+
--
77+
foreign import extname
78+
"var extname = require('path').extname;" :: FilePath -> FilePath
79+
80+
-- |
81+
-- The platform-specific file separator. `\\` or `/`.
82+
--
83+
foreign import sep
84+
"var sep = require('path').sep;" :: String
85+
86+
-- |
87+
-- The platform-specific path delimiter, `;` or `:`.
88+
--
89+
foreign import delimiter
90+
"var delimiter = require('path').delimiter;" :: String

tests/Simple.purs

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
module SimplePathTests where
2+
3+
import Control.Monad.Eff
4+
import Control.Monad.Eff.Exception
5+
import Debug.Trace
6+
import Node.Path
7+
8+
assertEq :: forall a. (Show a, Eq a) => a -> a -> Eff (err :: Exception String, trace :: Trace) {}
9+
assertEq x y =
10+
if x /= y
11+
then throwException $ "Assertion failed: " ++ show x ++ " /= " ++ show y
12+
else trace $ "Assertion passed"
13+
14+
main = do
15+
assertEq (normalize "/foo/bar//baz/asdf/quux/..") (normalize "/foo/bar/baz/asdf")
16+
assertEq (join "/foo" "bar") (normalize "/foo/bar")
17+
--assertEq (resolve ["foo/bar", "tmp/file/", ".."] "a/../subfile") (normalize "/foo/bar")
18+
assertEq (relative "/data/orandea/test/aaa" "/data/orandea/impl/bbb") (normalize "../../impl/bbb")
19+
assertEq (dirname "/foo/bar/baz/asdf/quux") (normalize "/foo/bar/baz/asdf")
20+
assertEq (basename "/foo/bar/baz/asdf/quux.html") "quux.html"
21+
assertEq (basenameWithoutExt "/foo/bar/baz/asdf/quux.html" ".html") "quux"
22+
assertEq (basenameWithoutExt "/foo/bar/baz/asdf/quux.txt" ".html") "quux.txt"
23+
assertEq (extname "index.html") ".html"
24+
assertEq (extname "index.coffee.md") ".md"
25+
assertEq (extname "index.") "."
26+
assertEq (extname "index") ""
27+
assertEq sep (normalize "/")
28+
assertEq (delimiter == ";" || delimiter == ":") true

0 commit comments

Comments
 (0)