// For given path returns root package path.
// If given path doesn't point to package content then null is returned.

'use strict';

var path          = require('path')
  , assertNotNull = require('es5-ext/lib/assert-not-null')
  , memoize       = require('es5-ext/lib/Function/memoize')
  , promisify     = require('deferred').promisify
  , stat          = promisify(require('fs').stat)
  , dirExists     = promisify(require('./fs/dir-exists'))
  , fileExists    = promisify(require('./fs/file-exists'))

  , basename = path.basename, dirname = path.dirname, resolve = path.resolve

  , isRoot, find;

isRoot = memoize(function (path) {
	return fileExists(resolve(path, 'package.json'))(function (exists) {
		return exists ? path : dirExists(resolve(path, 'node_modules'))(
			function (exists) {
				var parent;
				if (exists) {
					return path;
				} else {
					parent = dirname(path);
					if (parent === path) {
						return null;
					} else if (basename(parent) === 'node_modules') {
						return path;
					} else {
						return isRoot(parent);
					}
				}
			}
		);
	});
});

find = memoize(function (path) {
	return stat(path)(function (stats) {
		return isRoot(stats.isDirectory() ? path : dirname(path));
	}, function () {
		return isRoot(dirname(path));
	});
}, [function (path) {
	return assertNotNull(path) && resolve(String(path));
}]);

module.exports = function (path, callback) {
	try {
		return find(path).cb(callback);
	} catch (e) {
		callback(e);
	}
};

@@ -138,27 +138,24 @@
  path       
-   
 = require('p
@@ -164,29 +164,26 @@
 h')%0A  , 
-assertNotNull
+valid     
  = requi
@@ -202,23 +202,19 @@
 lib/
-assert-not-null
+valid-value
 ')%0A 
@@ -223,27 +223,24 @@
  memoize    
- 
-  
 = require('e
@@ -284,19 +284,16 @@
 misify  
-   
 = requir
@@ -331,19 +331,16 @@
 t       
-  
- 
 = promis
@@ -378,19 +378,16 @@
 Exists  
-   
 = promis
@@ -433,19 +433,16 @@
 eExists 
-   
 = promis
@@ -1279,21 +1279,13 @@
 urn 
-assertNotNull
+valid
 (pat

Update up to changes in es5-ext