Skip to content
This repository has been archived by the owner on Mar 7, 2023. It is now read-only.

Commit

Permalink
improvments
Browse files Browse the repository at this point in the history
  • Loading branch information
bdavidxyz committed Jan 4, 2017
1 parent 7d91c40 commit 5f8f51b
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 145 deletions.
62 changes: 21 additions & 41 deletions api/tests/unit/utils/lodash-utils_test.js
Original file line number Diff line number Diff line change
@@ -1,108 +1,88 @@
/* global describe, it, expect */

const _ = require('../../../lib/utils/lodash-utils'); // our custom function(s) of lodash
const _ = include('lib/utils/lodash-utils'); // our custom function(s) of lodash
const original_lodash = require('lodash');


describe('Unit | Utils | lodash-utils', function () {

describe('scope', function () {
it('should not affect original version of lodash', function (done) {
it('should not affect original version of lodash', function () {
expect(original_lodash.elementAfter).not.to.exist;
expect(_.elementAfter).to.exist;
done();
});
});

describe('elementAfter', function () {
it('for a given array and element in array (but not the last one), should return the element after the one provided', function (done) {
it('for a given array and element in array (but not the last one), should return the element after the one provided', function () {
expect(_.elementAfter(['a', 'b', 'c', 'd'], 'a')).to.equal('b');
expect(_.elementAfter(['a', 'b', 'c', 'd'], 'b')).to.equal('c');
expect(_.elementAfter(['a', 'b', 'c', 'd'], 'c')).to.equal('d');
done();
});
it('for a given array and the LAST element in array, should return undefined', function (done) {
it('for a given array and the LAST element in array, should return undefined', function () {
expect(_.elementAfter(['a', 'b', 'c', 'd'], 'd')).to.equal(undefined);
done();
});
it('for a given array and an element NOT in array, should return undefined', function (done) {
it('for a given array and an element NOT in array, should return undefined', function () {
expect(_.elementAfter(['a', 'b', 'c', 'd'], 'z')).to.equal(undefined);
done();
});
it('for an empty array, should return undefined', function (done) {
it('for an empty array, should return undefined', function () {
expect(_.elementAfter([], 'z')).to.equal(undefined);
done();
});
it('if first arg is not an array, should return undefined', function (done) {
it('if first arg is not an array, should return undefined', function () {
expect(_.elementAfter(new Date(), 'a')).to.equal(undefined);
done();
});
it('if last arg is missing, should return undefined', function (done) {
it('if last arg is missing, should return undefined', function () {
expect(_.elementAfter(['a', 'b', 'c', 'd'])).to.equal(undefined);
done();
});
it('if both args are is missing, should return undefined', function (done) {
it('if both args are is missing, should return undefined', function () {
expect(_.elementAfter()).to.equal(undefined);
done();
});
});


describe('areCSVequivalent', function () {
it('when no arg are given, should return false', function (done) {
it('when no arg are given, should return false', function () {
expect(_.areCSVequivalent()).to.equal(false);
done();
});
it('when two arg are given, but are not string, should return false', function (done) {
it('when two arg are given, but are not string, should return false', function () {
expect(_.areCSVequivalent(['1,2,3'], ['1,2,3'])).to.equal(false);
expect(_.areCSVequivalent(new Date(), new Date())).to.equal(false);
done();
});
it('when two string are the same, should return true', function (done) {
it('when two string are the same, should return true', function () {
expect(_.areCSVequivalent('1,2,3', '1,2,3')).to.equal(true);
expect(_.areCSVequivalent('azerty', 'azerty')).to.equal(true);
done();
});
it('when element are the same but in different order, should return true', function (done) {
it('when element are the same but in different order, should return true', function () {
expect(_.areCSVequivalent('1,2,3', '3,1,2')).to.equal(true);
done();
});
it('when element have space around values, should return true', function (done) {
it('when element have space around values, should return true', function () {
expect(_.areCSVequivalent('2 , blabla, 1', 'blabla ,1,2')).to.equal(true);
done();
});

});


describe('ensureString', function () {
it('when no input, return an empty String', function (done) {
it('when no input, return an empty String', function () {
expect(_.ensureString()).to.equal('');
done();
});
it('when input is explicitly undefined, return an empty String', function (done) {
it('when input is explicitly undefined, return an empty String', function () {
expect(_.ensureString(undefined)).to.equal('');
done();
});
it('when input is explicitly null, return an empty String', function (done) {
it('when input is explicitly null, return an empty String', function () {
expect(_.ensureString(null)).to.equal('');
done();
});
it('when input is a number (typeof meaning), it returns a toString() version of the input', function (done) {
it('when input is a number (typeof meaning), it returns a toString() version of the input', function () {
expect(_.ensureString(42)).to.equal('42');
done();
});
it('when input is a string (typeof meaning), it returns a toString() version of the input', function (done) {
it('when input is a string (typeof meaning), it returns a toString() version of the input', function () {
expect(_.ensureString('42')).to.equal('42');
done();
});
it('when input is an object (typeof meaning), it returns a toString() version of the input', function (done) {
it('when input is an object (typeof meaning), it returns a toString() version of the input', function () {
expect(_.ensureString(/[aeiou]+/g)).to.equal('/[aeiou]+/g');
done();
});
it('when input is an boolean (typeof meaning), it returns a toString() version of the input', function (done) {
it('when input is an boolean (typeof meaning), it returns a toString() version of the input', function () {
expect(_.ensureString(true)).to.equal('true');
done();
});
});
});
2 changes: 1 addition & 1 deletion live/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ You will need the following things properly installed on your computer.
* [Node.js](http://nodejs.org/) (with NPM)
* [Bower](http://bower.io/)
* [Ember CLI](http://ember-cli.com/)
* [PhantomJS](http://phantomjs.org/)
* [PhantomJS](http://phantomjs.org/) !! Be sure to install at least PhantomJS 2.X

## Installation

Expand Down
16 changes: 13 additions & 3 deletions live/app/models/answer/value-as-array-of-boolean-mixin.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
import Ember from 'ember';
// import _ from 'pix-live/utils/lodash-custom';
import stringToArrayOfBoolean from 'pix-live/utils/string-to-array-of-boolean';
import _ from 'pix-live/utils/lodash-custom';

export default Ember.Mixin.create({

/*
* Convert "1,2,4" into [true, true, false, true]
*/
_valueAsArrayOfBoolean: Ember.computed('value', function () {
return stringToArrayOfBoolean(this.get('value'));
return _.chain(this.get('value')) // in the worst case : ',4, 2 , 2,1, ,'
.checkPoint((e) => _.isString(e) ? e : '') // check if string
.split(',') // now ['', '4', ' 2 ', ' 2', '1', ' ', '']
.map(_.trim) // now ['', '4', '2', '2', '1', '', '']
.reject(_.isEmpty) // now ['4', '2', '2', '1']
.checkPoint((e) => _.every(e, _.isStrictlyPositiveInteger) ? e : []) // check if int >= 1
.map(_.parseInt) // now [4, 2, 2, 1]
.sortBy() // now [1, 2, 2, 4]
.uniqBy() // now [1, 2, 4]
.map((e) => e - 1) // now [0, 1, 3]
.thru((e) => _.times(_.max(e) + 1, (o) => _(e).includes(o)))
.value();
})

});
Expand Down
56 changes: 30 additions & 26 deletions live/app/utils/labeled-checkboxes.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,37 @@
import _ from 'pix-live/utils/lodash-custom';

// Example with 4 proposals
// Proposals could be ['prop 1','prop 2','prop 3','prop 4']
// Answers given are only the first(s) : [false, true], the missing values are false
// Output expected : [['prop 1', false], ['prop 2', true], ['prop 3', false], ['prop 4', false]]

function calculate(proposals, answers) {
// Example
// proposals = ['prop 1','prop 2','prop 3','prop 4']
// answers = [false, true]
const sizeDifference = proposals.length - answers.length;

return _.chain(answers) // [false, true]
.concat(_.times(sizeDifference, _.constant(false))) // [false, true, false, false]
.zip(proposals) // [[false, 'prop 1'], [true, 'prop 2'], [false, 'prop 3'], [false, 'prop 4']]
.map(_.reverse) // [['prop 1', false], ['prop 2', true], ['prop 3', false], ['prop 4', false]]
.value();
}

export default function labeledCheckboxes (proposals, answers) {

const DEFAULT_RETURN_VALUE = [];
/*
* Example :
* => Input :
* proposals : ['is sky red ?' , 'is sun red ?' , 'is grass red ?' , 'is cloud red ?']
* => Input :
* userAnswers : [false, true]
*
* WARNING : only first(s) userAnswers are given,
* all others have implicitly the boolean value "false"
*
* => Output :
* [['is sky red ?', false],
* ['is sun red ?', true],
* ['is grass red ?', false],
* ['are clouds red ?' false]]
*/
export default function labeledCheckboxes (proposals, userAnswers) {

// check pre-conditions
if (_(proposals).isEmpty()) return DEFAULT_RETURN_VALUE;
if (_(proposals).isNotArrayOfString()) return DEFAULT_RETURN_VALUE;
if (_(answers).isNotArrayOfBoolean()) return DEFAULT_RETURN_VALUE;

return calculate(proposals, answers);

if (_(proposals).isNotArrayOfString()) return [];
if (_(proposals).isEmpty()) return [];
if (_(userAnswers).isNotArrayOfBoolean()) return [];
if (_(userAnswers).size() > _(proposals).size()) return [];

const sizeDifference = _(proposals).size() - _(userAnswers).size(); // 2
const arrayOfFalse = _.times(sizeDifference, _.constant(false)); // [false, false]

return _.chain(userAnswers) // [false, true]
.concat(arrayOfFalse) // [false, true, false, false]
.zip(proposals) // [[false, 'prop 1'], [true, 'prop 2'], [false, 'prop 3'], [false, 'prop 4']]
.map(_.reverse) // [['prop 1', false], ['prop 2', true], ['prop 3', false], ['prop 4', false]]
.value();

}
20 changes: 0 additions & 20 deletions live/app/utils/string-to-array-of-boolean.js

This file was deleted.

54 changes: 0 additions & 54 deletions live/tests/unit/utils/string-to-array-of-boolean-test.js

This file was deleted.

0 comments on commit 5f8f51b

Please sign in to comment.