Skip to content

Instantly share code, notes, and snippets.

@zerobase
Last active May 21, 2020 06:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zerobase/12f89c8ba9f2a8fbdc91d53272d08078 to your computer and use it in GitHub Desktop.
Save zerobase/12f89c8ba9f2a8fbdc91d53272d08078 to your computer and use it in GitHub Desktop.
課税所得に対する所得税額を返す (計算根拠: 国税庁タックスアンサーNo.2260 所得税の税率 https://www.nta.go.jp/taxes/shiraberu/taxanswer/shotoku/2260.htm)
node_modules/
/**
* 課税所得に対する所得税額を返す (計算根拠: 国税庁タックスアンサーNo.2260 所得税の税率 https://www.nta.go.jp/taxes/shiraberu/taxanswer/shotoku/2260.htm)
* @param {number} taxableIncome 課税所得
* @returns {number} 所得税額
*/
function incomeTax(taxableIncome) {
if (typeof taxableIncome != 'number') {
throw TypeError('argument should be a number');
}
if (taxableIncome < 0 ) {
throw TypeError('argument should be equal or above 0');
}
const CalculatingTable = [
// [ threshold, taxRate(percent), deduction ]
[ 40000000, 45, 4796000],
[ 18000000, 40, 2796000],
[ 9000000, 33, 1536000],
[ 6950000, 23, 636000],
[ 3300000, 20, 427500],
[ 1950000, 10, 97500],
[ -1, 5, 0]
];
let matchedTuple = CalculatingTable.find(
function(tuple) {
let threshold = tuple[0];
if (taxableIncome > threshold) {
return true;
}
});
if (matchedTuple === undefined) {
throw 'Unknown error';
}
let taxRate = matchedTuple[1] / 100;
let deduction = matchedTuple[2];
return taxableIncome * taxRate - deduction;
}
module.exports = incomeTax;
{
"name": "income-tax-jp",
"version": "1.0.0",
"description": "課税所得に対する所得税額を返す (計算根拠: 国税庁タックスアンサーNo.2260 所得税の税率 https://www.nta.go.jp/taxes/shiraberu/taxanswer/shotoku/2260.htm)",
"main": "index.js",
"scripts": {
"test": "mocha"
},
"keywords": [],
"author": "Hide Ishi (https://github.com/zerobase)",
"license": "ISC",
"devDependencies": {
"mocha": "^6.1.4",
"should": "^13.2.3"
}
}
var incomeTax = require('./incomeTax.js');
require('should');
describe('incomeTax', function() {
it('should return positive number', function() {
let r = incomeTax(0);
r.should.be.a.Number;
r.should.aboveOrEqual(0);
});
it('should throw an error for non positive number', function() {
(function(){incomeTax(-1)}).should.throw(/argument should be equal or above 0/);
});
it('should throw an error for non number', function() {
(function(){incomeTax('1')}).should.throw(/argument should be a number/);
});
it('should return 974000 for 7000000', function() {
incomeTax(7000000).should.be.eql(974000);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment