1.0.2 • Published 7 years ago

ovl v1.0.2

Weekly downloads
13
License
-
Repository
-
Last release
7 years ago

Usage

const ovl = require("ovl").ovl
const func = ovl(overload_definition, default_function, option)
func(...any_arguments)

For the arguments, see the Example section

Examples

1

function isInt32(n){ return n === ~~n }
const obj = {a: 1}

const f1 = ovl([
	{
		type: ["number", "number", "number", "number"],
		this: obj,
		func(a, b, c, d){
			console.log(1, this, a, b, c, d)
		}
	},{
		type: ["string", "Date", "Array"],
		func: (a, b, c) => {
			console.log(2, a, b, c)
		}
	},{
		type: [isInt32, e => e > 100],
		func: (a, b) => {
			console.log(3, a, b)
		}
	},{
		type: (a, b, c) => a + b + c < 1,
		func: (a, b, c) => {
			console.log(4, a, b, c)
		}
	}
], (...args) => console.error("No matched function", args))


f1(1 ,2, 3, 4)
// 1 {a: 1} 1 2 3 4

f1("abc", new Date(), [100, 200])
// 2 "abc" 2017-03-25T06:48:20.127Z [ 100, 200 ]

f1(1, 2)
// No matched function [1, 2]

f1(1, 200)
// 3 1 200

f1(-10, -20, 30)
// 4 -10 -20 30

f1(10, 20, 30)
// No matched function [10, 20, 30]

2

const f2 = ovl({
	overloads: [
		{
			type: ["Array", "number"],
			func_name: "splitAt",
		},
		{
			type: ["Array"],
			func_name: "splitHalf",
		}
	],
	funcpool: {
		splitAt(arr, n){
			return [arr.slice(0, n), arr.slice(n)]
		},
		splitHalf(arr){
			return this.splitAt(arr, Math.ceil(arr.length / 2))
		},
	}
}, (...args) => console.error("No matched function", args))


console.log(f2([1, 2, 3, 4, 5, 6], 2))
// [ [ 1, 2 ], [ 3, 4, 5, 6 ] ]

console.log(f2([1, 2, 3, 4, 5]))
// [ [ 1, 2, 3 ], [ 4, 5 ] ]

3

const f3 = ovl([
	{
		type: ["number", "number", "number", "number"],
		this: obj,
		func(a, b, c, d){
			console.log(1, this, a, b, c, d)
			return "NNNN"
		}
	},{
		type: ["string", "Date", "Array"],
		func: (a, b, c) => {
			console.log(2, a, b, c)
			return "SDA"
		}
	},{
		type: [isInt32, e => e > 100],
		func: (a, b) => {
			console.log(3, a, b)
			return "IF"
		}
	},{
		type: (a, b, c) => a + b + c < 1,
		func: (a, b, c) => {
			console.log(4, a, b, c)
			return "XXX<1"
		}
	}
], (...args) => console.error("No matched function", args), {
	before: args => {console.log(args);return args},
	after: args => {console.log(args);return args},
	mode: "multi",
})


f3(-100, 200, -100, 200)
// [ -100, 200, -100, 200]          <-- before callback
// 1 {a: 1} -100 200 -100 200       <-- matched function
// 3 -100 200                       <-- matched function
// 4 -100 200 -100                  <-- matched function
// [ "NNNN", null, "IF", "XXX<1" ]  <-- after callback
// [ "NNNN", null, "IF", "XXX<1" ]  <-- return value

f3(100, 200)
// [ 100, 200]                 <-- before callback
// 3 100 200                   <-- matched function
// [ null, null, "IF", null ]  <-- after callback
// [ null, null, "IF", null ]  <-- return value

4

const f4 = ovl({
	overloads: [
		{
			type: ["Array", "number", "string"],
			func_name: "fn",
		},
		{
			type: {num: "number", arr: "Array", str: "string"},
			func_name: "fn2",
		}
	],
	funcpool: {
		fn(arr, num, str){
			return arr[num] === str
		},
		fn2({num, arr, str}){
			return this.fn(arr, num, str)
		},
	}
}, (...args) => console.error("No matched function", args))


console.log(f4(["a", "b", "c", "d"], 1, "b"))
// true

console.log(f4(["a", "b", "c", "d"], 3, "d"))
// true

console.log(f4(["a", "b", "c", "d"], 3, "b"))
// false

console.log(f4({num: 1, str: "def", arr: ["abc", "def", "ghi"]}))
// true

console.log(f4({num: 1, str: "xyz", arr: ["abc", "def", "ghi"]}))
// false

5

const f5 = ovl([
	{
		type: ["number?", "boolean"],
		func(a, b){
			console.log(1, a, b)
		}
	},{
		type: ["string|Date"],
		func: (d) => {
			console.log(2, new Date(d))
		}
	}
], (...args) => console.error("No matched function", args))


f5(1, true)
// 1 1 true

f5(null, true)
// 1 null true

f5(undefined, true)
// 1 undefined true

f5("2020-12-31")
// 2 2020-12-30T15:00:00.000Z

f5(new Date(2020, 11, 31))
// 2 2020-12-30T15:00:00.000Z

Matching

type: ["number", "number"]
arguments: [1, 2]
result: yes

type: ["number", "string"]
arguments: [1, 2]
result: no

type: ["number"]
arguments: [1, 2]
result: yes

type: ["number", function(x){ return x === 2 }]
arguments: [1, 2]
result: yes

type: ["number", function(x){ return x === 1 }]
arguments: [1, 2]
result: no

type: ["number", "number?"]
arguments: [1, null]
result: yes

type: ["number", "number"]
arguments: [1, null]
result: no

type: ["number", "number|string"]
arguments: [1, 1]
result: yes

type: ["number", "number|string"]
arguments: [1, "a"]
result: yes

type: function(a, b){return a === 1 && b === 2}
arguments: [1, 2]
result: yes

type: {a: "number", b: "string"}
arguments: [{a: 1, b: "a"}]
result: yes

type: {a: "number", b: "string"}
arguments: [{a: 1, b: 2}]
result: no

Change log

1.0.2 fix readme

1.0.1 add readme and fix meta data

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago