Skip to content

Commit 0a7cb03

Browse files
committed
updated the included types.js to 1.4.2
1 parent 1b048e4 commit 0a7cb03

File tree

5 files changed

+143
-117
lines changed

5 files changed

+143
-117
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,10 @@ __________
830830
change log
831831
==========
832832

833+
**1.2.2**
834+
835+
Updated the included types.js to (the current) version 1.4.2
836+
833837
**1.2.0**
834838

835839
Started improving the running-speed of all methods. It's a work in progress.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "strings.js",
3-
"version": "1.2.1",
3+
"version": "1.2.2",
44
"description": "A flexible, robust and powerful Javascript string manipulation library.",
55
"main": "strings.min.js",
66
"scripts": {

strings.coffee

Lines changed: 60 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -17,74 +17,79 @@
1717

1818
"use strict"
1919

20-
# types.coffee (types.js v1.3.6)
20+
# types.coffee (types.js v1.4.2)
2121

22-
emptyNumber= ->
23-
number= new Number
24-
number.void= true
25-
return number
22+
instanceOf = ( type, value ) -> value instanceof type
23+
# type defaults to object, for internal can do, saves for a few bytes..
24+
typeOf = ( value, type= 'object' ) -> typeof value is type
2625

27-
Types=
28-
parseIntBase: 10
29-
30-
literals=
26+
LITERALS=
3127
'Boolean' : false
3228
'String' : ''
33-
'Number' : emptyNumber()
3429
'Object' : {}
3530
'Array' : []
3631
'Function' : ->
32+
'Number' : do ->
33+
number= new Number
34+
number.void= true
35+
return number
36+
37+
TYPES=
38+
'Undefined' : ( value ) -> value is undefined
39+
'Null' : ( value ) -> value is null
40+
'Function' : ( value ) -> typeOf value, 'function'
41+
'Boolean' : ( value ) -> typeOf value, 'boolean'
42+
'String' : ( value ) -> typeOf value, 'string'
43+
'Array' : ( value ) -> typeOf(value) and instanceOf Array, value
44+
'RegExp' : ( value ) -> typeOf(value) and instanceOf RegExp, value
45+
'Date' : ( value ) -> typeOf(value) and instanceOf Date, value
46+
'Number' : ( value ) -> typeOf(value, 'number') and (value is value) or ( typeOf(value) and instanceOf(Number, value) )
47+
'Object' : ( value ) -> typeOf(value) and (value isnt null) and not instanceOf(Boolean, value) and not instanceOf(Number, value) and not instanceOf(Array, value) and not instanceOf(RegExp, value) and not instanceOf(Date, value)
48+
'NaN' : ( value ) -> typeOf(value, 'number') and (value isnt value)
49+
'Defined' : ( value ) -> value isnt undefined
50+
51+
TYPES.StringOrNumber= (value) -> TYPES.String(value) or TYPES.Number(value)
52+
53+
Types= _=
54+
# used by forceNumber to set the Radix, defaults to decimals
55+
parseIntBase: 10
3756

3857
createForce= ( type ) ->
39-
58+
# convert value in case initial type test failed. failed conversion returns false
4059
convertType= ( value ) ->
4160
switch type
42-
when 'Number' then return value if Types.isNumber value= parseInt value, Types.parseIntBase
43-
when 'String' then return value+ '' if Types.isStringOrNumber value
61+
when 'Number' then return value if (_.isNumber value= parseInt value, _.parseIntBase) and not value.void
62+
when 'String' then return value+ '' if _.isStringOrNumber value
4463
else return value if Types[ 'is'+ type ] value
45-
return false
4664

65+
# the forctType method, returns the type's defaultValue, if both value and replacement are not of, or convertible to, type
4766
return ( value, replacement ) ->
48-
return value if false isnt value= convertType value
49-
return replacement if false isnt replacement= convertType replacement
50-
return literals[ type ]
67+
return value if value? and undefined isnt value= convertType value
68+
return replacement if replacement? and undefined isnt replacement= convertType replacement
69+
return LITERALS[ type ]
5170

71+
# test multiple values(arguments) for a given predicate. returns breakState if predicate is breakState for some value
72+
# when no break occured, ! breakState will be returned.
5273
testValues= ( predicate, breakState, values= [] ) ->
53-
if values.length < 1
54-
return true if predicate is typesPredicates.Undefined
55-
return false
74+
# testing 'has' or 'all' for 'undefined' should return true on calls without arguments
75+
return ( predicate is TYPES.Undefined ) if values.length < 1
5676
for value in values
57-
return breakState if (predicate value) is breakState
77+
return breakState if predicate(value) is breakState
5878
return not breakState
5979

60-
typesPredicates=
61-
'Undefined' : (value) -> value is undefined
62-
'Null' : (value) -> value is null
63-
'Boolean' : (value) -> typeof value is 'boolean'
64-
'String' : (value) -> typeof value is 'string'
65-
'Function' : (value) -> typeof value is 'function'
66-
'Number' : (value) -> (typeof value is 'number') and (value is value) or ( (typeof value is 'object') and (value instanceof Number) and value.void )
67-
'Array' : (value) -> (typeof value is 'object') and (value instanceof Array)
68-
'RegExp' : (value) -> (typeof value is 'object') and (value instanceof RegExp)
69-
'Date' : (value) -> (typeof value is 'object') and (value instanceof Date)
70-
'Object' : (value) -> (typeof value is 'object') and (value isnt null) and not (value instanceof Array) and not (value instanceof RegExp) and not (value instanceof Date)
71-
'NaN' : (value) -> (typeof value is 'number') and (value isnt value)
72-
'Defined' : (value) -> value isnt undefined
73-
74-
typesPredicates.StringOrNumber= (value) -> typesPredicates['String'](value) or typesPredicates['Number'](value)
75-
80+
# generate all the is/not/has/all/force'Types'
7681
breakIfEqual= true
77-
do -> for name, predicate of typesPredicates then do ( name, predicate ) ->
82+
do -> for name, predicate of TYPES then do ( name, predicate ) ->
7883
Types[ 'is'+ name ] = predicate
7984
Types[ 'not'+ name ] = ( value ) -> not predicate value
8085
Types[ 'has'+ name ] = -> testValues predicate, breakIfEqual, arguments
8186
Types[ 'all'+ name ] = -> testValues predicate, not breakIfEqual, arguments
82-
Types[ 'force'+ name ]= createForce name if name of literals
87+
# create only forceType of types found in LITERALS
88+
Types[ 'force'+ name ]= createForce name if name of LITERALS
8389

8490
Types.typeof= ( value ) ->
85-
for type, predicate of typesPredicates
86-
return type.toLowerCase() if predicate(value) is true
87-
return 'unknown'
91+
for name, predicate of TYPES
92+
return name.toLowerCase() if predicate(value) is true
8893

8994
# end of types.coffee
9095

@@ -437,6 +442,17 @@ class Strings extends Chars
437442
ending= new RegExp Strings.regEscape( ending )+ '$'
438443
return ending.test string
439444

445+
# @formatNumber: ( number, interval= 3 ) ->
446+
# return number if '' is number= _.forceString number
447+
# formatted= ''
448+
# length= number.length- 1
449+
# interval= _.forceNumber interval, 3
450+
# for index in [0..length]
451+
# formatted+= number[ index ]
452+
# if ( (length- index)% interval is 0 ) and ( index < length )
453+
# formatted+= '.'
454+
# return formatted
455+
440456
# test below this line:
441457
@wrap: ( prepend= '', append= '' ) ->
442458
wrapper= ( string ) -> Strings.create prepend, string, append
@@ -536,6 +552,8 @@ class Strings extends Chars
536552

537553
endsWith: ( ending ) -> Strings.endsWith @string, ending
538554

555+
# formatNumber: ( formatting ) -> Strings.format @string, formatting
556+
539557
setWrap: ( prepend, append ) ->
540558
if _.isNull @wrapMethod then @wrapMethod= Strings.wrap prepend, append
541559
else @wrapMethod.wrap prepend, append

strings.js

Lines changed: 77 additions & 73 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)