Skip to content

Commit

Permalink
Merge pull request #11 from pat310/addUtilityFile
Browse files Browse the repository at this point in the history
Add utility file
  • Loading branch information
pat310 committed Jan 26, 2017
2 parents 94e2524 + 43fe855 commit 714ae1b
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 68 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
[![Build Status](https://img.shields.io/travis/pat310/quick-pivot.svg)](https://travis-ci.org/pat310/quick-pivot)
[![Coverage Status](https://coveralls.io/repos/github/pat310/quick-pivot/badge.svg?branch=addingCoveralls)](https://coveralls.io/github/pat310/quick-pivot?branch=addingCoveralls)
[![Code Climate](https://codeclimate.com/github/pat310/quick-pivot/badges/gpa.svg)](https://codeclimate.com/github/pat310/quick-pivot)
[![Dependency Status](https://img.shields.io/david/pat310/quick-pivot.svg?style=flat-square)](https://david-dm.org/pat310/quick-pivot)

## What it does
Say you have this example data set:<br>
Expand Down Expand Up @@ -91,9 +92,9 @@ pivot(dataArray, rows, columns, [accumulationCategory or CBfunction], [accumulat

#### First way to use it:
* `dataArray` **required** is one of the following:
* array of arrays ( the array in first index is assumed to be your headers, see the example above)
* array of objects (the keys of each object are the headers)
* a single array (a single column of data where the first element is the header)
* array of arrays ( the array in first index is assumed to be your headers, see the example above)
* array of objects (the keys of each object are the headers)
* a single array (a single column of data where the first element is the header)
* `rows` is an array of strings (the rows you want to pivot on) or an empty array **required**
* `columns` is an array of strings (the columns you want to pivot on) or an empty array **required**
* `accumulationCategory` is a string (the category you want to accumulate values for) **required**
Expand Down
68 changes: 4 additions & 64 deletions logic.js
Original file line number Diff line number Diff line change
@@ -1,69 +1,9 @@
'use strict';
//polyfill for Object.assign
function assignObject(target, varArgs) { // .length of function is 2
'use strict';
if (target === null) { // TypeError if undefined or null
throw new TypeError('Cannot convert undefined or null to object');
}

var to = Object(target);

for (var index = 1; index < arguments.length; index++) {
var nextSource = arguments[index];

if (nextSource !== null) { // Skip over if undefined or null
for (var nextKey in nextSource) {
// Avoid bugs when hasOwnProperty is shadowed
if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
to[nextKey] = nextSource[nextKey];
}
}
}
}
return to;
}

// polyfill for Array.fill
function arrayFill(value) {

// Steps 1-2.
if (this === null) {
throw new TypeError('this is null or not defined');
}

var O = Object(this);

// Steps 3-5.
var len = O.length >>> 0;

// Steps 6-7.
var start = arguments[1];
var relativeStart = start >> 0;

// Step 8.
var k = relativeStart < 0 ?
Math.max(len + relativeStart, 0) :
Math.min(relativeStart, len);

// Steps 9-10.
var end = arguments[2];
var relativeEnd = end === undefined ?
len : end >> 0;

// Step 11.
var final = relativeEnd < 0 ?
Math.max(len + relativeEnd, 0) :
Math.min(relativeEnd, len);

// Step 12.
while (k < final) {
O[k] = value;
k++;
}

// Step 13.
return O;
}
const {
arrayFill,
assignObject
} = require('./utils.js');

function fixDataFormat(data){
if(!Array.isArray(data) || !data.length) return [];
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "quick-pivot",
"version": "1.0.9",
"version": "1.0.10",
"description": "a utility for quickly pivoting data",
"main": "index.js",
"scripts": {
Expand Down
72 changes: 72 additions & 0 deletions utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
'use strict';

//polyfill for Object.assign
function assignObject(target, varArgs) { // .length of function is 2
'use strict';
if (target === null) { // TypeError if undefined or null
throw new TypeError('Cannot convert undefined or null to object');
}

var to = Object(target);

for (var index = 1; index < arguments.length; index++) {
var nextSource = arguments[index];

if (nextSource !== null) { // Skip over if undefined or null
for (var nextKey in nextSource) {
// Avoid bugs when hasOwnProperty is shadowed
if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
to[nextKey] = nextSource[nextKey];
}
}
}
}
return to;
}

// polyfill for Array.fill
function arrayFill(value) {

// Steps 1-2.
if (this === null) {
throw new TypeError('this is null or not defined');
}

var O = Object(this);

// Steps 3-5.
var len = O.length >>> 0;

// Steps 6-7.
var start = arguments[1];
var relativeStart = start >> 0;

// Step 8.
var k = relativeStart < 0 ?
Math.max(len + relativeStart, 0) :
Math.min(relativeStart, len);

// Steps 9-10.
var end = arguments[2];
var relativeEnd = end === undefined ?
len : end >> 0;

// Step 11.
var final = relativeEnd < 0 ?
Math.max(len + relativeEnd, 0) :
Math.min(relativeEnd, len);

// Step 12.
while (k < final) {
O[k] = value;
k++;
}

// Step 13.
return O;
}

module.exports = {
arrayFill: arrayFill,
assignObject: assignObject
};

0 comments on commit 714ae1b

Please sign in to comment.