-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #92 from ONSdigital/your-household-v4-lms
Your household v4 lms
- Loading branch information
Showing
54 changed files
with
9,541 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import {autoIncrementId} from './utils'; | ||
|
||
export const HOUSEHOLD_MEMBERS_STORAGE_KEY = 'household-members'; | ||
export const USER_HOUSEHOLD_MEMBER_ID = 'person_me'; | ||
export const HOUSEHOLD_MEMBER_TYPE = 'household-member'; | ||
export const VISITOR_TYPE = 'visitor'; | ||
|
||
/** | ||
* Types | ||
*/ | ||
export function person(opts) { | ||
if (opts.firstName === '' || opts.lastName === '') { | ||
console.log('Unable to create person with data: ', | ||
opts.firstName, | ||
!opts.middleName, | ||
!opts.lastName); | ||
} | ||
|
||
let middleName = opts.middleName || ''; | ||
|
||
return { | ||
fullName: opts.firstName + ' ' + middleName + ' ' + opts.lastName, | ||
firstName: opts.firstName, | ||
middleName, | ||
lastName: opts.lastName, | ||
gender: opts.gender | ||
}; | ||
} | ||
|
||
/** | ||
* Storage | ||
*/ | ||
export function getUserAsHouseholdMember() { | ||
return getAllHouseholdMembers().find((member) => { | ||
return member['@person'].id === USER_HOUSEHOLD_MEMBER_ID; | ||
}); | ||
} | ||
|
||
export function deleteUserAsHouseholdMember() { | ||
deleteHouseholdMember(USER_HOUSEHOLD_MEMBER_ID); | ||
} | ||
|
||
export function deleteHouseholdMember(personId) { | ||
let members = getAllHouseholdMembers().filter((member) => { | ||
return member['@person'].id !== personId; | ||
}); | ||
|
||
sessionStorage.setItem(HOUSEHOLD_MEMBERS_STORAGE_KEY, | ||
JSON.stringify(members)); | ||
} | ||
|
||
export function updateUserAsHouseholdMember(person, memberData) { | ||
let userAsHouseholdMember = getUserAsHouseholdMember(); | ||
|
||
userAsHouseholdMember | ||
? updateHouseholdMember(userAsHouseholdMember['@person'], memberData) | ||
: addHouseholdMember(person, memberData, USER_HOUSEHOLD_MEMBER_ID); | ||
} | ||
|
||
export function updateHouseholdMember(person, memberData) { | ||
let membersUpdated = getAllHouseholdMembers().map((member) => { | ||
return member['@person'].id === person.id | ||
? {...member, ...memberData, '@person': {...member['@person'], ...person}} | ||
: member; | ||
}); | ||
|
||
sessionStorage.setItem(HOUSEHOLD_MEMBERS_STORAGE_KEY, | ||
JSON.stringify(membersUpdated)); | ||
} | ||
|
||
export function addHouseholdMember(person, memberData, id) { | ||
let people = getAllHouseholdMembers() || []; | ||
memberData = memberData || {}; | ||
|
||
people.push({ | ||
...memberData, | ||
type: memberData.type || HOUSEHOLD_MEMBER_TYPE, | ||
'@person': { | ||
...person, | ||
id: id || 'person' + autoIncrementId('household-members') | ||
} | ||
}); | ||
|
||
sessionStorage.setItem(HOUSEHOLD_MEMBERS_STORAGE_KEY, JSON.stringify(people)); | ||
} | ||
|
||
export function getAllHouseholdMembers() { | ||
return JSON.parse(sessionStorage.getItem(HOUSEHOLD_MEMBERS_STORAGE_KEY)) || []; | ||
} | ||
|
||
export function getHouseholdMemberByPersonId(id) { | ||
return getAllHouseholdMembers().find(function(member) { | ||
return member['@person'].id === id; | ||
}); | ||
} | ||
|
||
export function getMemberPersonId(member) { | ||
return member['@person'].id; | ||
} | ||
|
||
/** | ||
* Comparators | ||
*/ | ||
export function isVisitor(member) { | ||
return member.type === window.ONS.storage.KEYS.VISITOR_TYPE; | ||
} | ||
|
||
export function isHouseholdMember(member) { | ||
return member.type === window.ONS.storage.KEYS.HOUSEHOLD_MEMBER_TYPE; | ||
} | ||
|
||
export function isOtherHouseholdMember(member) { | ||
return member.type === window.ONS.storage.KEYS.HOUSEHOLD_MEMBER_TYPE && | ||
member['@person'].id !== window.ONS.storage.IDS.USER_HOUSEHOLD_MEMBER_ID; | ||
} | ||
|
||
export const tempAwayQuestionSentenceMap = { | ||
'studying-away': 'who is working or studying away from home', | ||
'armed-forces': 'who is a member of the armed forces', | ||
'outside-uk': 'who is staying outside the UK for 12 months' | ||
}; |
146 changes: 146 additions & 0 deletions
146
_prototypes/your-household-lms/assets/numbers-to-words.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
/** | ||
* Copied from: | ||
* https://codereview.stackexchange.com/questions/90349/changing-number-to-words-in-javascript | ||
* =============== | ||
*/ | ||
var ONE_TO_NINETEEN = [ | ||
'one', 'two', 'three', 'four', 'five', | ||
'six', 'seven', 'eight', 'nine', 'ten', | ||
'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', | ||
'sixteen', 'seventeen', 'eighteen', 'nineteen' | ||
]; | ||
|
||
var TENS = [ | ||
'ten', 'twenty', 'thirty', 'forty', 'fifty', | ||
'sixty', 'seventy', 'eighty', 'ninety' | ||
]; | ||
|
||
var SCALES = ['thousand', 'million', 'billion', 'trillion']; | ||
|
||
// helper function for use with Array.filter | ||
function isTruthy(item) { | ||
return !!item; | ||
} | ||
|
||
// convert a number into 'chunks' of 0-999 | ||
function chunk(number) { | ||
var thousands = []; | ||
|
||
while(number > 0) { | ||
thousands.push(number % 1000); | ||
number = Math.floor(number / 1000); | ||
} | ||
|
||
return thousands; | ||
} | ||
|
||
// translate a number from 1-999 into English | ||
function inEnglish(number) { | ||
var thousands, hundreds, tens, ones, words = []; | ||
|
||
if(number < 20) { | ||
return ONE_TO_NINETEEN[number - 1]; // may be undefined | ||
} | ||
|
||
if(number < 100) { | ||
ones = number % 10; | ||
tens = number / 10 | 0; // equivalent to Math.floor(number / 10) | ||
|
||
words.push(TENS[tens - 1]); | ||
words.push(inEnglish(ones)); | ||
|
||
return words.filter(isTruthy).join('-'); | ||
} | ||
|
||
hundreds = number / 100 | 0; | ||
words.push(inEnglish(hundreds)); | ||
words.push('hundred'); | ||
words.push(inEnglish(number % 100)); | ||
|
||
return words.filter(isTruthy).join(' '); | ||
} | ||
|
||
// append the word for a scale. Made for use with Array.map | ||
function appendScale(chunk, exp) { | ||
var scale; | ||
if (!chunk) { | ||
return null; | ||
} | ||
scale = SCALES[exp - 1]; | ||
return [chunk, scale].filter(isTruthy).join(' '); | ||
} | ||
|
||
/** | ||
* =============== | ||
* End copy | ||
*/ | ||
|
||
/** | ||
* Modification - decorator | ||
*/ | ||
var NUMBER_TO_POSITION_TEXT_MAP = { | ||
'one': 'first', | ||
'two': 'second', | ||
'three': 'third', | ||
'four': 'fourth', | ||
'five': 'fifth', | ||
'six': 'sixth', | ||
'seven': 'seventh', | ||
'eight': 'eighth', | ||
'nine': 'nineth', | ||
'ten': 'tenth', | ||
'eleven': 'eleventh', | ||
'twelve': 'twelveth', | ||
'thirteen': 'thirteenth', | ||
'fourteen': 'fourteenth', | ||
'fifteen': 'fifteenth', | ||
'sixteen': 'sixteenth', | ||
'seventeen': 'seventeenth', | ||
'eighteen': 'eighteenth', | ||
'nineteen': 'nineteenth', | ||
|
||
'twenty': 'twentieth', | ||
'thirty': 'thirtieth', | ||
'forty': 'fortieth', | ||
'fifty': 'fiftieth', | ||
'sixty': 'sixtieth', | ||
'seventy': 'seventieth', | ||
'eighty': 'eightieth', | ||
'ninety': 'ninetieth', | ||
'hundred': 'hundredth', | ||
|
||
'thousand': 'thousandth', | ||
'million': 'millionth', | ||
'billion': 'billionth', | ||
'trillion': 'trillionth' | ||
}; | ||
|
||
export function numberToPositionWord(num) { | ||
const str = chunk(num) | ||
.map(inEnglish) | ||
.map(appendScale) | ||
.filter(isTruthy) | ||
.reverse() | ||
.join(' '); | ||
|
||
const sub = str.split(' '), | ||
lastWordDashSplitArr = sub[sub.length - 1].split('-'), | ||
lastWord = lastWordDashSplitArr[lastWordDashSplitArr.length - 1], | ||
|
||
newLastWord = (lastWordDashSplitArr.length > 1? lastWordDashSplitArr[0] + '-' : '') + | ||
NUMBER_TO_POSITION_TEXT_MAP[lastWord]; | ||
|
||
/*console.log('str:', str); | ||
console.log('sub:', sub); | ||
console.log('lastWordDashSplitArr:', lastWordDashSplitArr); | ||
console.log('lastWord:', lastWord); | ||
console.log('newLastWord:', newLastWord);*/ | ||
|
||
const subCopy = [].concat(sub); | ||
subCopy.pop(); | ||
const prefix = subCopy.join(' '); | ||
const result = (prefix ? prefix + ' ' : '') + newLastWord; | ||
|
||
// console.log('result', (prefix ? prefix + ' ' : '') + newLastWord); | ||
return result; | ||
} |
Oops, something went wrong.