Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
# Conflicts:
#	.gitignore
#	lib/readify.js
#	test/readify.js
  • Loading branch information
quazardous committed Jan 10, 2017
2 parents 7f11afd + 1a527be commit 3a6b4aa
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 22 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
.*
node_modules
legacy
npm-debug.log
.nyc_output
coverage
*.swp
/.project
/.*.md.html

2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2014-2016 coderaiser
Copyright (c) 2014-2017 coderaiser

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
35 changes: 20 additions & 15 deletions lib/readify.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const readdir = promisify(fs.readdir, fs);
const sortFiles = currify((attr, order, array) => {
const cmpCallbacks = {
'numeric': (a, b) => (+a - +b),
// http://www.jstips.co/en/sorting-strings-with-accented-characters/
'local_string': (a, b) => a.localeCompare(b),
'default': (a, b) => (a > b ? 1 : -1),
};
Expand Down Expand Up @@ -187,7 +188,7 @@ function parseStat(type, stat) {
size: format.size(size),
date: mtime,
owner: owner,
mode: mode && format.permissions.symbolic(mode),
mode: mode && format.permissions.symbolic(mode)
};
}

Expand All @@ -209,7 +210,7 @@ function fillJSON(path, stats, options, callback) {
return callback(null, json);

changeUIDToName(json, (error, files) => {
json.files = files;
json.files = files || json.files;
callback(null, json);
});
}
Expand All @@ -219,21 +220,12 @@ function changeUIDToName(json, callback) {
callback(null, json.files);
else
nicki((error, names) => {
let files;

if (error)
files = json.files.slice();
else
files = json.files.map((file) => {
const owner = file.owner;

if (names[owner])
file.owner = names[owner];

return file;
});
return callback(error);

const files = replaceFromList(names, 'owner', json.files);

callback(error, files);
callback(null, files);
});
}

Expand All @@ -256,3 +248,16 @@ function changeOrder(json) {
return sorted;
}

function replaceFromList(obj, prop, array) {
return array.map((a) => {
const n = a[prop];
const data = obj[n];

if (!data)
return a;

return Object.assign(a, {
[prop]: data
});
})
}
Empty file added test/fixture/accents/a.txt
Empty file.
Empty file added test/fixture/accents/d.txt
Empty file.
Empty file added test/fixture/accents/e.txt
Empty file.
Empty file added test/fixture/accents/f.txt
Empty file.
Empty file added test/fixture/accents/z.txt
Empty file.
Empty file added test/fixture/accents/è.txt
Empty file.
Empty file added test/fixture/accents/é.txt
Empty file.
72 changes: 67 additions & 5 deletions test/readify.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,29 @@ test('result: should be sorted by name folders then files', (t) => {
});
});

test('readify: result', (t) => {
test('readify sort: accent', (t) => {
const files = [
'a.txt',
'd.txt',
'e.txt',
'é.txt',
'è.txt',
'f.txt',
'z.txt'
];

const dir = path.join(__dirname, 'fixture', 'accents');
readify(dir, (error, data) => {
const names = data.files.map((file) => {
return file.name;
});

t.deepEqual(names, files, 'should use correct order for accents');
t.end();
});
});

test('readify: result: no owner', (t) => {
const update = () => {
delete require.cache[require.resolve('..')];
readify = require('..');
Expand Down Expand Up @@ -122,6 +144,49 @@ test('readify: result', (t) => {
});
});

test('readify: result: owner', (t) => {
const update = () => {
delete require.cache[require.resolve('..')];
readify = require('..');
};

const {readdir, stat} = fs;

const name = 'hello.txt';
const mode = 16893;
const size = 1024;
const mtime = new Date('2016-11-23T14:36:46.311Z');
const uid = 2;

fs.readdir = (dir, fn) => {
fn(null, [name]);
};

fs.stat = (name, fn) => {
fn(null, {
isDirectory: noop,
name,
mode,
size,
mtime,
uid
});
};

update();

readify('.', (error, result) => {
t.ok(result.files[0].owner, 'should contain owner');

fs.readdir = readdir;
fs.stat = stat;

update();

t.end();
});
});

test('readify: result: "raw"', (t) => {
const update = () => {
delete require.cache[require.resolve('..')];
Expand Down Expand Up @@ -316,10 +381,7 @@ test('readify stat: error', (t) => {
const dir = path.resolve(__dirname, '..', 'dist');
readify(dir, (error, data) => {
t.notOk(error, 'no error when stat error');
data.files = data.files.map(function(file) {
delete file.raw;
return file;
});

t.deepEqual(data.files, files, 'size, date, owner, mode should be empty');

fs.stat = stat;
Expand Down

0 comments on commit 3a6b4aa

Please sign in to comment.