Skip to content

Commit

Permalink
Chore: refactor with classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
mysticatea committed Jun 1, 2017
1 parent 8f00ebb commit ffc7fcb
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 81 deletions.
76 changes: 40 additions & 36 deletions lib/util/cache.js
Expand Up @@ -16,49 +16,53 @@ const SKIP_TIME = 5000
//------------------------------------------------------------------------------

/**
* A class of cache.
* The class of cache.
* The cache will dispose of each value if the value has not been accessed
* during 5 seconds.
*
* @constructor
*/
const Cache = module.exports = function Cache() {
this.map = Object.create(null)
}
module.exports = class Cache {
/**
* Initialize this cache instance.
*/
constructor() {
this.map = new Map()
}

Object.defineProperties(Cache.prototype, {
get: {
value: function get(key) {
const entry = this.map[key]
const now = Date.now()
/**
* Get the cached value of the given key.
* @param {any} key The key to get.
* @returns {any} The cached value or null.
*/
get(key) {
const entry = this.map.get(key)
const now = Date.now()

if (entry && entry.expire - now > 0) {
if (entry) {
if (entry.expire > now) {
entry.expire = now + SKIP_TIME
return entry.value
}
return null
},
configurable: true,
writable: true,
},
this.map.delete(key)
}
return null
}

put: {
value: function put(key, value) {
const entry = this.map[key]
const now = Date.now()
/**
* Set the value of the given key.
* @param {any} key The key to set.
* @param {any} value The value to set.
* @returns {void}
*/
set(key, value) {
const entry = this.map.get(key)
const expire = Date.now() + SKIP_TIME

if (entry) {
entry.value = value
entry.expire = now + SKIP_TIME
}
else {
this.map[key] = {
value,
expire: now + SKIP_TIME,
}
}
},
configurable: true,
writable: true,
},
})
if (entry) {
entry.value = value
entry.expire = expire
}
else {
this.map.set(key, {value, expire})
}
}
}
10 changes: 2 additions & 8 deletions lib/util/check-publish.js
Expand Up @@ -53,14 +53,10 @@ module.exports = function checkForPublish(context, filePath, targets) {
Object.create(null),
packageInfo.devDependencies || {}
)
let i = 0
let target = null

if (npmignore.match(toRelative(filePath))) {
// This file is private, so this can import private files.
for (i = 0; i < targets.length; ++i) {
target = targets[i]

for (const target of targets) {
if (target.moduleName &&
!dependencies[target.moduleName] &&
!devDependencies[target.moduleName] &&
Expand All @@ -77,9 +73,7 @@ module.exports = function checkForPublish(context, filePath, targets) {
}
else {
// This file is published, so this cannot import private files.
for (i = 0; i < targets.length; ++i) {
target = targets[i]

for (const target of targets) {
if (target.moduleName ?
(!dependencies[target.moduleName] && allowed.indexOf(target.moduleName) === -1) :
npmignore.match(toRelative(target.filePath))
Expand Down
2 changes: 1 addition & 1 deletion lib/util/get-npmignore.js
Expand Up @@ -171,7 +171,7 @@ module.exports = function getNpmignore(startPath) {
retv.match = and(filterNeverIgnoredFiles(p), or(notAncestorFiles, npmignoreIgnore))
}

cache.put(p.filePath, retv)
cache.set(p.filePath, retv)
}

return retv
Expand Down
8 changes: 4 additions & 4 deletions lib/util/get-package-json.js
Expand Up @@ -67,15 +67,15 @@ module.exports = function getPackageJson(startPath) {
data = cache.get(dir)
if (data) {
if (dir !== startDir) {
cache.put(startDir, data)
cache.set(startDir, data)
}
return data
}

data = readPackageJson(dir)
if (data) {
cache.put(dir, data)
cache.put(startDir, data)
cache.set(dir, data)
cache.set(startDir, data)
return data
}

Expand All @@ -85,6 +85,6 @@ module.exports = function getPackageJson(startPath) {
}
while (dir !== prevDir)

cache.put(startDir, null)
cache.set(startDir, null)
return null
}
67 changes: 35 additions & 32 deletions lib/util/import-target.js
Expand Up @@ -123,39 +123,42 @@ function getModuleName(nameOrPath) {

/**
* Information of an import target.
*
* @constructor
* @param {ASTNode} node - The node of a `require()` or a module declaraiton.
* @param {string} name - The name of an import target.
* @param {string} basedir - The path of base directory to resolve relative path.
* @param {string[]} exts - Extensions that it checks whether or not the file exists.
*/
module.exports = function ImportTarget(node, name, basedir, exts) {
const relative = /^\./.test(name)

/**
* The node of a `require()` or a module declaraiton.
* @type {ASTNode}
*/
this.node = node

module.exports = class ImportTarget {
/**
* The name of this import target.
* @type {string}
* Initialize this instance.
* @param {ASTNode} node - The node of a `require()` or a module declaraiton.
* @param {string} name - The name of an import target.
* @param {string} basedir - The path of base directory to resolve relative path.
* @param {string[]} exts - Extensions that it checks whether or not the file exists.
*/
this.name = name

/**
* The full path of this import target.
* If the target is a module then this is `null`.
* @type {string|null}
*/
this.filePath = relative ? resolve(basedir, name, exts) : null

/**
* The module name of this import target.
* If the target is a relative path then this is `null`.
* @type {string|null}
*/
this.moduleName = relative ? null : getModuleName(name)
constructor(node, name, basedir, exts) {
const relative = /^\./.test(name)

/**
* The node of a `require()` or a module declaraiton.
* @type {ASTNode}
*/
this.node = node

/**
* The name of this import target.
* @type {string}
*/
this.name = name

/**
* The full path of this import target.
* If the target is a module then this is `null`.
* @type {string|null}
*/
this.filePath = relative ? resolve(basedir, name, exts) : null

/**
* The module name of this import target.
* If the target is a relative path then this is `null`.
* @type {string|null}
*/
this.moduleName = relative ? null : getModuleName(name)
}
}

0 comments on commit ffc7fcb

Please sign in to comment.