@knoword/common

Common elements shared between Knoword projects

Stats

StarsIssuesVersionUpdatedCreatedSize
@knoword/common
7.2.22 years ago4 years agoMinified + gzip package size for @knoword/common in KB

Readme

@knoword/common

Build Status

Useful constants and utility functions that we share between Knoword projects

Installation

$ npm install @knoword/common

Usage

import {DIFFICULTIES, PARTS_OF_SPEECH} from '@knoword/common';

console.log(PARTS_OF_SPEECH); // ['noun', 'verb', 'adjective', 'adverb']

if (DIFFICULTIES.includes('easy')) {
  alert('It works!'); // It really does
}

Using variables

import {FONT_SIZE_LARGEST, GRAY_LIGHT} from '@knoword/common/variables';
import styled from 'styled-components';

const Heading = styled.h1`
  font-size: ${FONT_SIZE_LARGEST}px;
  color: ${GRAY_LIGHT};
`;

Encoding/decoding words

On the server:

const {encode} = require('@knoword/common/encoder');

encode('apple'); // U=YXbGBw

On the client:

import {decode} from '@knoword/common/encoder';

decode('U=YXbGBw'); // apple

Evaluating a guess

import evaluateGuess from '@knoword/common/evaluateGuess';

const guess = 'apPle';
const solutions = ['U=YXbGBw'];
evaluateGuess(guess, solutions);
/*
{
  match: 'apple',
  correctCount: 5,
  correctPercent: 1,
  incorrect: false,
  preparedSolution: 'apPle'
}
*/

Answer utility functions

During a game, we will keep track of answers given by the user. We can determine a number of things based on these, including streaks, accuracy, points, and messaging. Each of the following functions take an array of booleans, representing the correct status of each answer and return something useful.

import {
  getCurrentStreak,
  getLongestStreak,
  getPoints,
  getAccuracy,
  getMessage
} from '@knoword/common/answerUtils';

// Suppose our game state looked something like this:
const state = {
  answers: [
    {
      id: 1,
      correct: true
    },
    {
      id: 2,
      correct: true
    },
    {
      id: 3,
      correct: false
    },
    {
      id: 4,
      correct: true
    }
  ]
};

// Complex answer state must be coaxed into an array of booleans
const answers = state.answers.map(answer => answer.correct);

const currentStreak = getCurrentStreak(answers); // 1
const longestStreak = getLongestStreak(answers); // 2
const points = getPoints(answers); // 25
const accuracy = getAccuracy(answers); // 0.75
const message = getMessage(answers); // 'Great job'

If you find any bugs or have a feature request, please open an issue on github!

The npm package download data comes from npm's download counts api and package details come from npms.io.