Skip to content

Commit

Permalink
- Enhancement: Return Promise
Browse files Browse the repository at this point in the history
- Refactoring: Switch to ES6 (const/let, for-of, destructuring)
- License: Rename to reflect type (MIT)
  • Loading branch information
brettz9 committed May 21, 2019
1 parent b4e50e3 commit 3bb597d
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 41 deletions.
File renamed without changes.
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ Install:

Usage:

```
var mdls = require('mdls')
mdls('./index.js', function(err, data) {
console.log(err, data)
})
```js
try {
const data = await mdls('./index.js');
console.log('Data', data);
} catch (err) {
console.log('Error', err);
}
```

If all goes well, this will log:
Expand Down Expand Up @@ -57,8 +58,8 @@ If all goes well, this will log:
ItemPhysicalSize: 4096 }
```

Each date above is a javascript date object. You can get the unix timestamp out
with the .getTime method of each object.
Each date above is a JavaScript date object. You can get the Unix timestamp out
with the `getTime` method of each object.

Note that running `mdls` the command line utility would have returned:

Expand Down
56 changes: 28 additions & 28 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,45 @@
var path = require('path')
, exec = require('child_process').exec

var BAD_VALUE = new Object
const path = require('path')
, {exec} = require('child_process')

module.exports = mdls

function mdls(file, ready) {
var file = path.resolve(file).replace(/ /g, '\\ ')

exec('mdls ' + file, function(err, raw_data) {
if(err) {
ready(err)
}

ready(null, deserialize(raw_data))
})
file = path.resolve(file).replace(/ /g, '\\ ')

return new Promise((resolve, reject) => {
exec('mdls ' + file, function(err, raw_data) {
if(err) {
if (ready) ready(err)
reject(err);
return;
}

const deserialized = deserialize(raw_data);
if (ready) ready(null, deserialized)
resolve(deserialized);
})
});
}

function deserialize(raw_data) {
var splits = raw_data.split('\n') // only targets osx
const splits = raw_data.split('\n') // only targets osx
, lines = []
, data = {}

for(var i = 0, len = splits.length; i < len; ++i) {
if(splits[i].indexOf('=') === - 1) {
lines[lines.length - 1] = lines[lines.length - 1] + splits[i].trim()
for (const split of splits) {
if(!split.includes('=')) {
lines[lines.length - 1] += split.trim()

continue
}

lines[lines.length] = splits[i].trim()
lines[lines.length] = split.trim()
}

var value
, key
, kv

for(var i = 0, len = lines.length; i < len; ++i) {
kv = lines[i].split('=')
key = kv[0].trim().replace('kMD', '')
value = kv[1].trim()
for (const line of lines) {
const kv = line.split('=')
const key = kv[0].trim().replace('kMD', '')
let value = kv[1].trim()

if(value === '(null)') {
value = null;
Expand All @@ -64,13 +64,13 @@ function to_js_type(key) {
return value.slice(1, -1)
}

var as_num = +value
const as_num = +value

if(!isNaN(as_num)) {
return as_num
}

var as_date = new Date(value)
const as_date = new Date(value)

if(isNaN(as_date.getTime())) {
bad_value(key, value)
Expand Down
14 changes: 9 additions & 5 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
var mdls = require('./index.js')

mdls('./index.js', function(err, data) {
console.log(err, data)
})
const mdls = require('./index.js');

(async () => {

try {
const data = await mdls('./index.js');
console.log('Data', data);
} catch (err) {
console.log('Error', err);
}

})();

0 comments on commit 3bb597d

Please sign in to comment.