Skip to content
This repository has been archived by the owner on Jan 18, 2024. It is now read-only.

Commit

Permalink
First work.
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesplease committed Jan 29, 2015
1 parent 1699f9c commit 7d25857
Show file tree
Hide file tree
Showing 16 changed files with 495 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ build/Release
# Commenting this out is preferred by some people, see
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git-
node_modules
bower_components
coverage
tmp

# Users Environment Variables
.lock-wscript
66 changes: 66 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{
"bitwise" : true,
"camelcase" : true,
"curly" : true,
"eqeqeq" : true,
"forin" : false,
"immed" : true,
"indent" : 2,
"latedef" : true,
"newcap" : true,
"noarg" : true,
"noempty" : true,
"nonbsp" : true,
"nonew" : true,
"plusplus" : false,
"quotmark" : "single",
"undef" : true,
"unused" : true,
"strict" : false,
"trailing" : true,
"maxparams" : 4,
"maxdepth" : 2,
"maxstatements" : 15,
"maxcomplexity" : 10,
"maxlen" : 100,

"asi" : false,
"boss" : false,
"debug" : false,
"eqnull" : false,
"esnext" : true,
"evil" : false,
"expr" : false,
"funcscope" : false,
"globalstrict" : false,
"iterator" : false,
"lastsemic" : false,
"laxbreak" : false,
"laxcomma" : false,
"loopfunc" : false,
"maxerr" : 50,
"multistr" : false,
"notypeof" : false,
"proto" : false,
"scripturl" : false,
"smarttabs" : false,
"shadow" : false,
"sub" : false,
"supernew" : false,
"validthis" : false,
"noyield" : false,

"browser" : true,
"couch" : false,
"devel" : false,
"dojo" : false,
"jquery" : false,
"mootools" : false,
"node" : false,
"nonstandard" : false,
"prototypejs" : false,
"rhino" : false,
"worker" : false,
"wsh" : false,
"yui" : false
}
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
language: node_js
node_js:
- "0.10"
script: "gulp coverage"
after_success:
- npm install -g codeclimate-test-reporter
- codeclimate < coverage/lcov.info
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
# moment-business.js
Utilities for working with week days and work days in Moment. (Only supports western countries)
# moment-business
[![Travis build status](http://img.shields.io/travis/jmeas/moment-business.svg?style=flat)](https://travis-ci.org/jmeas/moment-business)
[![Code Climate](https://codeclimate.com/github/jmeas/moment-business/badges/gpa.svg)](https://codeclimate.com/github/jmeas/moment-business)
[![Test Coverage](https://codeclimate.com/github/jmeas/moment-business/badges/coverage.svg)](https://codeclimate.com/github/jmeas/moment-business)
[![Dependency Status](https://david-dm.org/jmeas/moment-business.svg)](https://david-dm.org/jmeas/moment-business)
[![devDependency Status](https://david-dm.org/jmeas/moment-business/dev-status.svg)](https://david-dm.org/jmeas/moment-business#info=devDependencies)

31 changes: 31 additions & 0 deletions bower.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "moment-business",
"version": "0.0.1",
"homepage": "https://github.com/jmeas/moment-business",
"authors": [
"Jmeas <jellyes2@gmail.com>"
],
"description": "Methods to work with week days and weekends in moment.",
"main": "dist/moment-business.js",
"keywords": [
"moment",
"momentjs",
"moment.js",
"business",
"workday",
"weekday",
"workweek",
"saturday",
"sunday",
"weekend",
"weekends"
],
"license": "MIT",
"ignore": [
"**/.*",
"node_modules",
"bower_components",
"test",
"tests"
]
}
9 changes: 9 additions & 0 deletions config/index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"destinationFolder": "dist",
"exportFileName": "moment-business",
"entryFileName": "moment-business",
"mochaGlobals": [
"stub", "spy", "expect",
"moment", "containedPeriodicValues"
]
}
125 changes: 125 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
var gulp = require('gulp');
var $ = require('gulp-load-plugins')({
replaceString: /^gulp(-|\.)([0-9]+)?/
});
const del = require('del');
const browserify = require('browserify');
const runSequence = require('run-sequence');
const source = require('vinyl-source-stream');

// Adjust this file to configure the build
const config = require('./config');

// Remove the built files
gulp.task('clean', function(cb) {
del([config.destinationFolder], cb);
});

// Remove our temporary files
gulp.task('clean:tmp', function(cb) {
del(['tmp'], cb);
});

// Send a notification when JSHint fails,
// so that you know your changes didn't build
function ding(file) {
return file.jshint.success ? false : 'JSHint failed';
};

// Lint our source code
gulp.task('lint:src', function() {
return gulp.src(['src/**/*.js', '!src/wrapper.js'])
.pipe($.jshint())
.pipe($.jshint.reporter('jshint-stylish'))
.pipe($.notify(ding))
.pipe($.jshint.reporter('fail'));
});

// Lint our test code
gulp.task('lint:test', function() {
return gulp.src(['test/unit/**/*.js'])
.pipe($.jshint())
.pipe($.jshint.reporter('jshint-stylish'))
.pipe($.notify(ding))
.pipe($.jshint.reporter('fail'));
});

// Build two versions of the library
gulp.task('build', ['lint:src', 'clean'], function() {
return gulp.src('src/wrapper.js')
.pipe($.template(config))
.pipe($.preprocess())
.pipe($.rename(config.exportFileName + '.js'))
.pipe($.sourcemaps.init())
.pipe($.to5({blacklist: ['useStrict'], modules: 'ignore'}))
.pipe($.sourcemaps.write('./'))
.pipe(gulp.dest(config.destinationFolder))
.pipe($.filter(['*', '!**/*.js.map']))
.pipe($.rename(config.exportFileName + '.min.js'))
.pipe($.uglifyjs({
outSourceMap: true,
inSourceMap: config.destinationFolder + '/' + config.exportFileName + '.js.map',
}))
.pipe(gulp.dest(config.destinationFolder));
});

// Use 6to5 to build the library to CommonJS modules. This
// is fed to Browserify, which builds the version of the lib
// for our browser spec runner.
gulp.task('compile_browser_script', function() {
return gulp.src(['src/**/*.js', '!src/wrapper.js'])
.pipe($.to5({modules: 'common'}))
.pipe(gulp.dest('tmp'))
.pipe($.filter([config.entryFileName + '.js']))
.pipe($.rename('__entry.js'))
.pipe(gulp.dest('tmp'));
});

// Bundle our app for our unit tests
gulp.task('browserify', ['compile_browser_script'], function() {
var bundleStream = browserify(['./test/setup/browserify.js']).bundle();
return bundleStream
.pipe(source('./tmp/__spec-build.js'))
.pipe(gulp.dest(''))
.pipe($.livereload());
});

gulp.task('coverage', function(done) {
gulp.src(['src/*.js', '!src/wrapper.js'])
.pipe($.istanbul())
.on('finish', function() {
return test()
.pipe($.istanbul.writeReports())
.on('end', done);
});
});

function test() {
return gulp.src(['test/setup/node.js', 'test/unit/**/*.js'], {read: false})
.pipe($.mocha({reporter: 'dot', globals: config.mochaGlobals}));
};

// Lint and run our tests
gulp.task('test', ['lint:src', 'lint:test'], function() {
require('6to5/register')({ modules: 'common' });
return test();
});

// Ensure that linting occurs before browserify runs. This prevents
// the build from breaking due to poorly formatted code.
gulp.task('build_in_sequence', function(callback) {
runSequence(['lint:src', 'lint:test'], 'browserify', callback);
});

// This is used when testing in the browser. Reloads the tests
// when the lib, or the tests themselves, change.
gulp.task('watch', function() {
$.livereload.listen({port: 35729, host: 'localhost', start: true});
gulp.watch(['src/**/*.js', 'test/**/*', '.jshintrc', 'test/.jshintrc', 'config/index.json'], ['build_in_sequence']);
});

// Set up a livereload environment for our spec runner
gulp.task('test:browser', ['build_in_sequence', 'watch']);

// An alias of test
gulp.task('default', ['test']);
61 changes: 61 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{
"name": "moment-business",
"version": "0.0.1",
"description": "Methods to work with week days and weekends in moment.",
"main": "dist/moment-business.js",
"scripts": {
"test": "gulp"
},
"repository": {
"type": "git",
"url": "https://github.com/jmeas/moment-business.git"
},
"keywords": [
"moment",
"momentjs",
"moment.js",
"business",
"workday",
"workweek",
"saturday",
"sunday",
"weekend",
"weekends"
],
"author": "Jmeas",
"license": "MIT",
"bugs": {
"url": "https://github.com/jmeas/moment-business/issues"
},
"homepage": "https://github.com/jmeas/moment-business",
"devDependencies": {
"6to5": "^2.12.6",
"browserify": "^8.1.1",
"chai": "^1.10.0",
"del": "^1.1.1",
"gulp": "^3.8.10",
"gulp-6to5": "^2.0.2",
"gulp-filter": "^2.0.0",
"gulp-istanbul": "git://github.com/jmeas/gulp-istanbul.git#isparta",
"gulp-jshint": "^1.9.0",
"gulp-livereload": "^3.4.0",
"gulp-load-plugins": "^0.8.0",
"gulp-mocha": "^2.0.0",
"gulp-notify": "^2.1.0",
"gulp-preprocess": "^1.2.0",
"gulp-rename": "^1.2.0",
"gulp-sourcemaps": "^1.3.0",
"gulp-template": "^2.0.0",
"gulp-uglifyjs": "^0.5.0",
"jshint-stylish": "^1.0.0",
"mocha": "^2.1.0",
"run-sequence": "^1.0.2",
"sinon": "^1.12.2",
"sinon-chai": "^2.6.0",
"vinyl-source-stream": "^1.0.0"
},
"dependencies": {
"contained-periodic-values": "^0.0.1",
"moment": "^2.9.0"
}
}
19 changes: 19 additions & 0 deletions src/moment-business.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import moment from 'moment';
import containedPeriodicValues from 'contained-periodic-values';

moment.fn.weekDays = function(start) {
var startDay = start.day();
var totalDistance = this.diff(start, 'days');

// Calculate the nearest Saturday and Sunday to our start day
var containedSundays = containedPeriodicValues(startDay, 0, 7);
var containedSaturdays = containedPeriodicValues(startDay, 6, 7);

return totalDistance - containedSaturdays + containedSundays;
}

moment.fn.weekendDays = function(start) {
var workdays = this.weekDays(start);
var totalDistance = this.diff(start, 'days');
return totalDistance - workDays;
}
15 changes: 15 additions & 0 deletions src/wrapper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
define(['moment', 'contained-periodic-values'], factory);
} else if (typeof exports !== 'undefined') {
var moment = require('moment');
var containedPeriodicValues = require('contained-periodic-values');
module.exports = factory();
} else {
factory(root.moment, root.containedPeriodicValues);
}
})(this, function(moment, containedPeriodicValues) {
'use strict';

// @include ./moment-business.js
});
Loading

0 comments on commit 7d25857

Please sign in to comment.