Socket
Socket
Sign inDemoInstall

question-store

Package Overview
Dependencies
Maintainers
1
Versions
48
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

question-store - npm Package Compare versions

Comparing version 0.3.0 to 0.3.1

263

index.js

@@ -12,2 +12,3 @@ /*!

var util = require('util');
var path = require('path');
var Options = require('option-cache');

@@ -37,2 +38,6 @@ var Question = require('./lib/question');

if (!this.options.name && !this.options.dest) {
throw new Error('expected "options.name" or "options.dest" to be a string');
}
this.inquirer = this.options.inquirer || utils.inquirer;

@@ -85,14 +90,29 @@ this.enqueued = false;

/**
* Get the answer object for question `name`, for the current locale and cwd.
* Add a question that will have its answer stored as a default
* value.
*
* ```js
* questions.setDefault('author.name', 'What is your name?');
* ```
* @param {String} `name`
* @param {Object} `val`
* @param {Object} `options`
* @api public
*/
Questions.prototype.setDefault = function(name, val, options) {
var question = this.set.apply(this, arguments);
question.options.isDefault = true;
return this;
};
/**
* Get question `name`, or group `name` if question is not found.
* You can also do a direct lookup using `quesions.cache['foo']`.
*
* ```js
* var name = questions.get('name');
* //=> {name: 'Jon'}
*
* // specify a locale
* var name = questions.get('name', 'fr');
* //=> {name: 'Jean'}
* //=> question object
* ```
* @param {String} `name`
* @param {String} `locale`
* @return {Object} Returns the question object.

@@ -135,54 +155,68 @@ * @api public

/**
* Create a question group from the given key. This is used in `addQuestion`
* to add namespaced questions to groups.
* Get the answer for question `name` at the current cwd.
*
* Optionally specify a locale to get, otherwise the default locale's
* answer is returend.
*
* ```js
* questions
* .set('author.name', 'Author name?')
* .set('author.url', 'Author url?')
* var name = questions.getAnswer('name');
* //=> {name: 'Jon'}
*
* // console.log(questions.groups);
* //=> {author: ['author.name', 'author.url']}
* // specify a locale
* var name = questions.getAnswer('name', 'fr');
* //=> {name: 'Jean'}
* ```
* @param {String} `key`
* @return {Object}
* @param {String} `name`
* @param {String} `locale`
* @return {Object} Returns the question object.
* @api public
*/
Questions.prototype.group = function(key) {
var segs = key.split('.');
var name = segs[0];
var item = segs[1];
if (!item) return this;
this.groups[name] = this.groups[name] || [];
utils.union(this.groups[name], [key]);
this.groupMap[key] = name;
return this;
Questions.prototype.getAnswer = function(name, locale) {
var question = this.get(name);
if (question) {
return question.get(locale);
}
};
/**
* Get a group with the given `key`. If `key` has a dot, only the substring
* before the dot is used for the lookup.
* Get the default answer object for question `name` for the
* current locale. Optionally specify a locale to get the
* default answer for that locale.
*
* ```js
* questions
* .set('author.name', 'Author name?')
* .set('author.url', 'Author url?')
* .set('project.name', 'Project name?')
* .set('project.url', 'Project url?')
* var name = questions.getDefaultAnswer('name');
* //=> {name: 'Jon'}
*
* var group = questions.getGroup('author');
* //=> ['author.name', 'author.url']
* // specify a locale
* var name = questions.getDefaultAnswer('name', 'fr');
* //=> {name: 'Jean'}
* ```
* @param {String} `name`
* @param {String} `locale`
* @return {Object} Returns the question object.
* @api public
*/
Questions.prototype.getDefaultAnswer = function(name, locale) {
var question = this.get(name);
if (question) {
return question.getDefault(locale);
}
};
/**
* Return true if question `name` has been answered for the current locale
* and the current working directory.
*
* questions.ask(group, function(err, answers) {
* // do stuff with answers
* });
* ```js
* question.isAnswered(locale);
* ```
* @param {String} `key`
* @return {Object}
* @param {String} `name` Question name
* @param {String} `locale` Optionally pass a locale
* @api public
*/
Questions.prototype.getGroup = function(key) {
return this.groups[key.split('.').shift()];
Questions.prototype.isAnswered = function(name, locale) {
return this.question(name).isAnswered(locale);
};

@@ -246,21 +280,2 @@

/**
* Add a question that, when answered, will save the value as the default
* value to be used for the current locale.
*
* ```js
* questions.setDefault('author.name', 'What is your name?');
* ```
* @param {String} `name`
* @param {Object} `val`
* @param {Object} `options`
* @api public
*/
Questions.prototype.setDefault = function(name, val, options) {
var question = this.addQuestion.apply(this, arguments);
question.options.isDefault = true;
return this;
};
/**
* Get the `question` instance stored for the given `name`. This is the entire

@@ -281,3 +296,3 @@ * `Question` object, with all answers for all locales and directories.

}
var question = this.get(name);
var question = this.get(name, this.options.locale);
if (typeof question === 'undefined') {

@@ -297,2 +312,3 @@ throw new Error('question-store cannot find question "' + name + '"');

var question = new Question(name, val, opts);
question.dest = this.dest;
question.cwd = this.cwd;

@@ -337,15 +353,54 @@

/**
* Return true if question `name` has been answered for the current locale
* and the current working directory.
* Create a question group from the given key. This is used in `addQuestion`
* to add namespaced questions to groups.
*
* ```js
* question.isAnswered(locale);
* questions
* .set('author.name', 'Author name?')
* .set('author.url', 'Author url?')
*
* // console.log(questions.groups);
* //=> {author: ['author.name', 'author.url']}
* ```
* @param {String} `name` Question name
* @param {String} `locale` Optionally pass a locale
* @param {String} `key`
* @return {Object}
*/
Questions.prototype.group = function(key) {
var segs = key.split('.');
var name = segs[0];
var item = segs[1];
if (!item) return this;
this.groups[name] = this.groups[name] || [];
utils.union(this.groups[name], [key]);
this.groupMap[key] = name;
return this;
};
/**
* Get a group with the given `key`. If `key` has a dot, only the substring
* before the dot is used for the lookup.
*
* ```js
* questions
* .set('author.name', 'Author name?')
* .set('author.url', 'Author url?')
* .set('project.name', 'Project name?')
* .set('project.url', 'Project url?')
*
* var group = questions.getGroup('author');
* //=> ['author.name', 'author.url']
*
* questions.ask(group, function(err, answers) {
* // do stuff with answers
* });
* ```
* @param {String} `key`
* @return {Object}
* @api public
*/
Questions.prototype.isAnswered = function(name, locale) {
return this.question(name).isAnswered(locale);
Questions.prototype.getGroup = function(key) {
return this.groups[key.split('.').shift()];
};

@@ -413,22 +468,2 @@

/**
* Enqueue one or more questions to be asked.
*
* @return {String}
*/
Questions.prototype.enqueue = function(names) {
names = utils.arrayify(names).reduce(function(acc, name) {
return acc.concat(this.groups[name] || name);
}.bind(this), []);
// clear the queue if this is the first time `enqueue` is called
if (!this.enqueued) {
this.enqueued = true;
this.queue = [];
}
this.queue = utils.union(this.queue, names);
return this;
};
/**
* Ask one or more questions, with the given `options` and callback.

@@ -558,2 +593,28 @@ *

/**
* Enqueue one or more questions to be asked.
*
* @return {String}
*/
Questions.prototype.enqueue = function(names, options) {
names = [].concat.apply([], [].slice.call(arguments));
if (utils.isObject(names[names.length - 1])) {
options = names.pop();
}
names.reduce(function(acc, name) {
return acc.concat(this.groups[name] || name);
}.bind(this), []);
// clear the queue if this is the first time `enqueue` is called
if (!this.enqueued) {
this.enqueued = true;
this.queue = [];
}
this.queue = utils.union(this.queue, names);
return this;
};
/**
* Remove a question from the queue.

@@ -592,2 +653,26 @@ *

/**
* Getter/setter for answer dest
*/
Object.defineProperty(Questions.prototype, 'dest', {
set: function(dest) {
this.paths.dest = dest;
},
get: function() {
if (this.paths.dest) {
return this.paths.dest;
}
if (this.options.dest) {
var dest = path.resolve(utils.resolveDir(this.options.dest));
return (this.paths.dest = dest);
}
var name = path.join(utils.gm, this.options.name, 'answers');
var dest = path.resolve(utils.resolveDir(name));
return (this.paths.dest = dest);
}
});
/**
* Getter/setter for answer cwd

@@ -594,0 +679,0 @@ */

@@ -203,3 +203,3 @@ /*!

if (this.isAnswered(opts.locale) && !opts.force === true) {
if (this.isAnswered(opts.locale) && opts.force !== true) {
cb(null, utils.toAnswer(opts.name, answer));

@@ -215,3 +215,3 @@ return;

if (!opts.default) {
if (typeof opts.default === 'undefined' || opts.default === null) {
delete opts.default;

@@ -222,9 +222,10 @@ }

this.inquirer.prompt(opts, function(answer) {
var val = utils.get(answer, opts.name);
if (opts.isDefault === true) {
this.setDefault(answer);
this.setDefault(val);
}
if (opts.save !== false) {
this.set(answer);
if (opts.save !== false && !opts.isDefault && val !== opts.default) {
this.set(val);
}
cb(null, utils.toAnswer(opts.name, answer));
cb(null, utils.set({}, opts.name, val));
}.bind(this));

@@ -231,0 +232,0 @@ }.bind(this));

@@ -11,2 +11,3 @@ 'use strict';

require = utils;
require('global-modules', 'gm');
require('define-property', 'define');

@@ -13,0 +14,0 @@ require('extend-shallow', 'extend');

{
"name": "question-store",
"description": "Ask questions, persist the answers. Basic support for i18n and storing answers based on current working directory.",
"version": "0.3.0",
"version": "0.3.1",
"homepage": "https://github.com/jonschlinkert/question-store",

@@ -29,14 +29,16 @@ "author": "Jon Schlinkert (https://github.com/jonschlinkert)",

"extend-shallow": "^2.0.1",
"get-value": "^2.0.0",
"get-value": "^2.0.2",
"global-modules": "^0.2.0",
"has-value": "^0.3.0",
"inquirer2": "github:jonschlinkert/inquirer2",
"isobject": "^2.0.0",
"lazy-cache": "^0.2.4",
"lazy-cache": "^1.0.2",
"object-visit": "^0.3.4",
"option-cache": "^3.2.0",
"resolve-dir": "^0.1.0",
"set-value": "^0.3.1",
"set-value": "^0.3.2",
"use": "^1.1.2"
},
"devDependencies": {
"micromatch": "^2.3.5",
"mocha": "*"

@@ -56,2 +58,5 @@ },

},
"plugins": [
"gulp-format-md"
],
"reflinks": [

@@ -61,2 +66,2 @@ "question-cache"

}
}
}

@@ -1,2 +0,2 @@

# question-store [![NPM version](https://badge.fury.io/js/question-store.svg)](http://badge.fury.io/js/question-store)
# question-store [![NPM version](https://img.shields.io/npm/v/question-store.svg)](https://www.npmjs.com/package/question-store)

@@ -36,3 +36,3 @@ > Ask questions, persist the answers. Basic support for i18n and storing answers based on current working directory.

### [Questions](index.js#L24)
### [Questions](index.js#L27)

@@ -51,3 +51,3 @@ Create an instance of `Questions` with the given `options`.

### [.set](index.js#L71)
### [.set](index.js#L82)

@@ -78,10 +78,11 @@ Cache a question to be asked at a later point. Creates an instance of [Question](#question), so any `Question` options or settings may be used.

### [.setData](index.js#L90)
### [.setDefault](index.js#L100)
Set data to be used for answering questions, or as default answers when `force` is true.
Add a question that will have its answer stored as a default value.
**Params**
* `key` **{String|Object}**: Property name to set, or object to extend onto `questions.data`
* `val` **{any}**: The value to assign to `key`
* `name` **{String}**
* `val` **{Object}**
* `options` **{Object}**

@@ -91,15 +92,13 @@ **Example**

```js
questions.setData('foo', 'bar');
// or
questions.setData({foo: 'bar'});
questions.setDefault('author.name', 'What is your name?');
```
### [.hasData](index.js#L110)
### [.get](index.js#L119)
Return true if property `key` has a value on `questions.data`.
Get question `name`, or group `name` if question is not found. You can also do a direct lookup using `quesions.cache['foo']`.
**Params**
* `key` **{String}**: The property to lookup.
* `returns` **{Boolean}**
* `name` **{String}**
* `returns` **{Object}**: Returns the question object.

@@ -109,13 +108,14 @@ **Example**

```js
questions.hasData('abc');
var name = questions.get('name');
//=> question object
```
### [.getData](index.js#L127)
### [.del](index.js#L134)
Get the value of property `key` from `questions.data`.
Delete the answer for question `name` for the current (or given) locale.
**Params**
* `key` **{String}**: The property to get.
* `returns` **{any}**: Returns the value of property `key`
* `name` **{String}**: Question name
* `locale` **{String}**: Optionally pass a locale

@@ -125,16 +125,17 @@ **Example**

```js
questions.setData('foo', 'bar');
questions.getData('foo');
//=> 'bar'
question.del(locale);
```
### [.setDefault](index.js#L144)
### [.getAnswer](index.js#L171)
Add a question that, when answered, will save the value as the default value to be used for the current locale.
Get the answer for question `name` at the current cwd.
Optionally specify a locale to get, otherwise the default locale's
answer is returend.
**Params**
* `name` **{String}**
* `val` **{Object}**
* `options` **{Object}**
* `locale` **{String}**
* `returns` **{Object}**: Returns the question object.

@@ -144,8 +145,13 @@ **Example**

```js
questions.setDefault('author.name', 'What is your name?');
var name = questions.getAnswer('name');
//=> {name: 'Jon'}
// specify a locale
var name = questions.getAnswer('name', 'fr');
//=> {name: 'Jean'}
```
### [.get](index.js#L183)
### [.getDefaultAnswer](index.js#L197)
Get the answer object for question `name`, for the current locale and cwd.
Get the default answer object for question `name` for the current locale. Optionally specify a locale to get the default answer for that locale.

@@ -161,12 +167,76 @@ **Params**

```js
var name = questions.get('name');
var name = questions.getDefaultAnswer('name');
//=> {name: 'Jon'}
// specify a locale
var name = questions.get('name', 'fr');
var name = questions.getDefaultAnswer('name', 'fr');
//=> {name: 'Jean'}
```
### [.question](index.js#L199)
### [.isAnswered](index.js#L216)
Return true if question `name` has been answered for the current locale and the current working directory.
**Params**
* `name` **{String}**: Question name
* `locale` **{String}**: Optionally pass a locale
**Example**
```js
question.isAnswered(locale);
```
### [.setData](index.js#L234)
Set data to be used for answering questions, or as default answers when `force` is true.
**Params**
* `key` **{String|Object}**: Property name to set, or object to extend onto `questions.data`
* `val` **{any}**: The value to assign to `key`
**Example**
```js
questions.setData('foo', 'bar');
// or
questions.setData({foo: 'bar'});
```
### [.hasData](index.js#L254)
Return true if property `key` has a value on `questions.data`.
**Params**
* `key` **{String}**: The property to lookup.
* `returns` **{Boolean}**
**Example**
```js
questions.hasData('abc');
```
### [.getData](index.js#L271)
Get the value of property `key` from `questions.data`.
**Params**
* `key` **{String}**: The property to get.
* `returns` **{any}**: Returns the value of property `key`
**Example**
```js
questions.setData('foo', 'bar');
questions.getData('foo');
//=> 'bar'
```
### [.question](index.js#L287)
Get the `question` instance stored for the given `name`. This is the entire `Question` object, with all answers for all locales and directories.

@@ -185,10 +255,9 @@

### [.isAnswered](index.js#L222)
### [.delQuestion](index.js#L328)
Return true if question `name` has been answered for the current locale and the current working directory.
Delete a question.
**Params**
* `name` **{String}**: Question name
* `locale` **{String}**: Optionally pass a locale
* `name` **{String}**: The question to delete.

@@ -198,13 +267,13 @@ **Example**

```js
question.isAnswered(locale);
question.deleteQuestion(name);
```
### [.del](index.js#L237)
### [.getGroup](index.js#L396)
Delete the answer for question `name` for the current (or given) locale.
Get a group with the given `key`. If `key` has a dot, only the substring before the dot is used for the lookup.
**Params**
* `name` **{String}**: Question name
* `locale` **{String}**: Optionally pass a locale
* `key` **{String}**
* `returns` **{Object}**

@@ -214,6 +283,17 @@ **Example**

```js
question.del(locale);
questions
.set('author.name', 'Author name?')
.set('author.url', 'Author url?')
.set('project.name', 'Project name?')
.set('project.url', 'Project url?')
var group = questions.getGroup('author');
//=> ['author.name', 'author.url']
questions.ask(group, function(err, answers) {
// do stuff with answers
});
```
### [.deleteAll](index.js#L264)
### [.deleteAll](index.js#L410)

@@ -232,3 +312,3 @@ Delete answers for all questions for the current (or given) locale.

### [.erase](index.js#L282)
### [.erase](index.js#L428)

@@ -247,3 +327,3 @@ Erase all answers for question `name` from the file system.

### [.ask](index.js#L327)
### [.ask](index.js#L473)

@@ -266,5 +346,37 @@ Ask one or more questions, with the given `options` and callback.

### [.getIndex](index.js#L576)
Get a the index of question `name` from the queue.
**Params**
* `name` **{String}**
* `returns` **{Object}**
**Example**
```js
questions.getIndex('author');
//=> 1
```
### [.unqueue](index.js#L621)
Remove a question from the queue.
**Params**
* `items` **{Object}**: Object of views
**Example**
```js
console.log(questions.queue);
//=> ['a', 'b', 'c'];
questions.unqueue('a');
```
### Question
### [Question](lib/question.js#L24)
### [Question](lib/question.js#L25)

@@ -284,3 +396,3 @@ Create new `Question` store `name`, with the given `options`.

### [.set](lib/question.js#L64)
### [.set](lib/question.js#L66)

@@ -299,3 +411,3 @@ Set the answer to the question for the current (or given) locale, at the current working directory.

### [.get](lib/question.js#L80)
### [.get](lib/question.js#L82)

@@ -314,3 +426,3 @@ Get the answer for the current (or given) locale for the current working directory.

### [.del](lib/question.js#L95)
### [.del](lib/question.js#L97)

@@ -329,3 +441,3 @@ Delete the answer for the current (or given) locale and the current working directory.

### [.erase](lib/question.js#L109)
### [.erase](lib/question.js#L111)

@@ -340,3 +452,3 @@ Delete the answer store (all answers for the question) from the file system.

### [.isAnswered](lib/question.js#L125)
### [.isAnswered](lib/question.js#L127)

@@ -355,3 +467,3 @@ Return true if the question has been answered for the current locale and the current working directory.

### [.setDefault](lib/question.js#L140)
### [.setDefault](lib/question.js#L142)

@@ -370,3 +482,3 @@ Set the default answer to use for the current (or given) locale, at the current working directory.

### [.getDefault](lib/question.js#L155)
### [.getDefault](lib/question.js#L157)

@@ -385,3 +497,3 @@ Get the default answer for the current (or given) locale

### [.hasDefault](lib/question.js#L171)
### [.hasDefault](lib/question.js#L173)

@@ -400,3 +512,3 @@ Return true if the question has been given a default answer for the current (or given) locale, at the current working directory.

### [.ask](lib/question.js#L192)
### [.ask](lib/question.js#L194)

@@ -454,8 +566,8 @@ Ask the question.

+ [github/jonschlinkert](https://github.com/jonschlinkert)
+ [twitter/jonschlinkert](http://twitter.com/jonschlinkert)
* [github/jonschlinkert](https://github.com/jonschlinkert)
* [twitter/jonschlinkert](http://twitter.com/jonschlinkert)
## License
Copyright © 2015 Jon Schlinkert
Copyright © 2015 [Jon Schlinkert](https://github.com/jonschlinkert)
Released under the MIT license.

@@ -465,2 +577,2 @@

_This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on November 27, 2015._
_This file was generated by [verb](https://github.com/verbose/verb) on December 13, 2015._
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc