Skip to content

Commit

Permalink
fix: fixes deprecation warning (closes #8, closes #11, closes #7)
Browse files Browse the repository at this point in the history
  • Loading branch information
niftylettuce committed Jan 2, 2020
1 parent 590c382 commit 76baf11
Show file tree
Hide file tree
Showing 5 changed files with 1,039 additions and 1,144 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
language: node_js
node_js:
- '8'
- '10'
- '12'
services: mongodb
after_success:
npm run coverage
13 changes: 6 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ const getUniqueSlug = async (config, constructor, _id, str, i = 0) => {
return getUniqueSlug(config, constructor, _id, str, i + 1);
};

const mongooseSlugPlugin = (schema, config = {}) => {
config = {
const mongooseSlugPlugin = (schema, options = {}) => {
const config = {
tmpl: '',
locals: {},
alwaysUpdateSlug: true,
Expand All @@ -29,7 +29,7 @@ const mongooseSlugPlugin = (schema, config = {}) => {
i18n: false,
slugOptions: {},
paranoid: false,
...config
...options
};

const obj = {};
Expand All @@ -41,14 +41,13 @@ const mongooseSlugPlugin = (schema, config = {}) => {
trim: true,
set: val => config.slug(val, config.slugOptions),
validate: {
isAsync: true,
validator(val, fn) {
validator(val) {
const message =
config.i18n && config.i18n.t && this.locale
? config.i18n.t(config.errorMessage, this.locale)
: config.errorMessage;
if (!isSANB(val)) return fn(false, message);
fn(true);
if (!isSANB(val)) return Promise.reject(message);
Promise.resolve(true);
}
}
};
Expand Down
18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,20 @@
"speakingurl": "^14.0.1"
},
"devDependencies": {
"@commitlint/cli": "^8.2.0",
"@commitlint/config-conventional": "^8.2.0",
"@commitlint/cli": "^8.3.3",
"@commitlint/config-conventional": "^8.3.3",
"ava": "^2.4.0",
"codecov": "^3.6.1",
"cross-env": "^6.0.3",
"dayjs": "^1.8.16",
"eslint": "^6.5.1",
"dayjs": "^1.8.18",
"eslint": "^6.8.0",
"eslint-config-xo-lass": "^1.0.3",
"fixpack": "^2.3.1",
"husky": "^3.0.8",
"lint-staged": "^9.4.2",
"mongoose": "^5.6.11",
"nyc": "^14.1.1",
"remark-cli": "^7.0.0",
"husky": "^3.1.0",
"lint-staged": "^9.5.0",
"mongoose": "^5.8.3",
"nyc": "^15.0.0",
"remark-cli": "^7.0.1",
"remark-preset-github": "^0.0.16",
"xo": "^0.25.3"
},
Expand Down
59 changes: 12 additions & 47 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,53 +63,18 @@ test('increment slugs', async t => {
t.is(smartBlogPost.slug, `${blogPost.slug}-2`);
});

/*
test('custom error message', async t => {
const BlogPost = new mongoose.Schema({ title: String });
BlogPost.plugin(mongooseSlugPlugin, { tmpl: '<%=title%>' });
const BlogPosts = mongoose.model('BlogPost', BlogPost);
const title = `Foo${new Date().getTime()}`;
const blogPost = await BlogPosts.create({ title });
t.is(blogPost.slug, slug(title));
});
test('custom slug function', async t => {
const BlogPost = new mongoose.Schema({ title: String });
BlogPost.plugin(mongooseSlugPlugin, { tmpl: '<%=title%>' });
const BlogPosts = mongoose.model('BlogPost', BlogPost);
const title = `Foo${new Date().getTime()}`;
const blogPost = await BlogPosts.create({ title });
t.is(blogPost.slug, slug(title));
});
test('custom slug field', async t => {
const BlogPost = new mongoose.Schema({ title: String });
BlogPost.plugin(mongooseSlugPlugin, { tmpl: '<%=title%>' });
const BlogPosts = mongoose.model('BlogPost', BlogPost);
const title = `Foo${new Date().getTime()}`;
const blogPost = await BlogPosts.create({ title });
t.is(blogPost.slug, slug(title));
});
test('custom slug history', async t => {
const BlogPost = new mongoose.Schema({ title: String });
BlogPost.plugin(mongooseSlugPlugin, { tmpl: '<%=title%>' });
const BlogPosts = mongoose.model('BlogPost', BlogPost);
const title = `Foo${new Date().getTime()}`;
const blogPost = await BlogPosts.create({ title });
t.is(blogPost.slug, slug(title));
});
test('support i18n translation using this.locale', async t => {
const BlogPost = new mongoose.Schema({ title: String });
BlogPost.plugin(mongooseSlugPlugin, { tmpl: '<%=title%>' });
const BlogPosts = mongoose.model('BlogPost', BlogPost);
const title = `Foo${new Date().getTime()}`;
const blogPost = await BlogPosts.create({ title });
t.is(blogPost.slug, slug(title));
const Schema = new mongoose.Schema({ title: String });
Schema.plugin(mongooseSlugPlugin, {
tmpl: '<%=title%>',
errorMessage: 'A custom error message'
});
const Model = mongoose.model('Custom', Schema);
const err = await t.throwsAsync(() => Model.create({}));
t.is(err.message, 'A custom error message');
});

test('custom slug options', async t => {
// TODO: finish this
});
*/
test.todo('custom slug function');
test.todo('custom slug field');
test.todo('custom slug history');
test.todo('custom slug options');
Loading

0 comments on commit 76baf11

Please sign in to comment.