Skip to content

Commit

Permalink
Release 0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
shootingfly committed May 23, 2020
1 parent 908582f commit fb7ed1d
Show file tree
Hide file tree
Showing 13 changed files with 216 additions and 37 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
/bin/
/.shards/
*.dwarf

/.vscode/
.DS_Store
/tmp/
# Libraries don't need dependency lock
# Dependencies will be locked in applications that use them
/shard.lock
7 changes: 3 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
language: crystal

# Uncomment the following if you'd like Travis to run specs and check code formatting
# script:
# - crystal spec
# - crystal tool format --check
script:
- crystal tool format --check
- crystal spec
37 changes: 23 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,39 +1,48 @@
# suggest
# What Happen

TODO: Write a description here
Tell you what happen when x changes to y.

Work on methods without any arguments or all arguments have default values.

## Installation

1. Add the dependency to your `shard.yml`:

```yaml
dependencies:
suggest:
github: your-github-user/suggest
```
```yaml
dependencies:
what_happen:
github: shootingfly/what_happen
```
2. Run `shards install`

## Usage

```crystal
require "suggest"
```
require "what_happen"
TODO: Write usage instructions here
puts what_happen(from: [1, 2, 3], to: 1) # => ["first", "first?", "min", "min?"]
```

## Development
## Expection

TODO: Write development instructions here
It doesn't work on the following methods.
```crystal
["transpose", "to_h", "sample", "sum", "product"]
```

## Contributing

1. Fork it (<https://github.com/your-github-user/suggest/fork>)
1. Fork it (<https://github.com/shootingfly/what_happen/fork>)
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create a new Pull Request

## Contributors

- [Shootingfly](https://github.com/your-github-user) - creator and maintainer
- [Shootingfly](https://github.com/shootingfly) - creator and maintainer

## Thanks

Thanks to jbodah for his awesome work on [suggest_rb](https://github.com/jbodah/suggest_rb)
4 changes: 2 additions & 2 deletions shard.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
name: suggest
name: what_happen
version: 0.1.0

authors:
- Shootingfly <[email protected]>
- Shootingfly

crystal: 0.34.0

Expand Down
14 changes: 14 additions & 0 deletions spec/exaple_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require "./spec_helper"

describe WhatHappen do
it "full example" do
what_happen(from: false, to: "false").should eq ["to_s", "inspect", "pretty_inspect"]
what_happen(from: 1, to: -1).should eq ["-"]
what_happen(from: {1, 2, 3}, to: 1).should eq ["first", "first?", "min", "min?"]
what_happen(from: {a: 1}, to: {:a}).should eq ["keys", "sorted_keys"]
what_happen(from: [1, 2], to: 1).should eq ["first", "first?", "min", "min?"]
what_happen(from: [1, 2, 3], to: 3).should eq ["size", "last", "last?", "max", "max?"]
what_happen(from: "hello", to: "HELLO").should eq ["upcase"]
what_happen(from: "Hello", to: "hello").should eq ["downcase", "underscore"]
end
end
6 changes: 5 additions & 1 deletion spec/spec_helper.cr
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
require "spec"
require "../src/suggest"
require "../src/what_happen"

macro after(method, expression, *, become)
what_happen(from: {{ expression }}, to: {{ become }}).should contain {{ method.id.stringify }}
end
9 changes: 0 additions & 9 deletions spec/suggest_spec.cr

This file was deleted.

95 changes: 95 additions & 0 deletions spec/what_happen_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
require "./spec_helper"
require "big"

describe WhatHappen do
it "not work with SKIP_METHODS" do
# See `WhatHappen::SKIP_METHODS`
what_happen(from: [1, 2, 3], to: 5).should eq [] of String # product
what_happen(from: [1, 2, 3], to: 6).should eq [] of String # sum
what_happen(from: [1, 2, 3, 4], to: 2).should eq [] of String # sample
end

it "works with Bool" do
after to_s, true, become: "true"
end

it "works with Enumerable" do
after any?, [1, 2, 3], become: true
after any?, ["1", 2, 3], become: true
after first, [1, 2, 3], become: 1
after first?, [1, 2, 3], become: 1
after max, [1, 2, 3], become: 3
after max?, [1, 2, 3], become: 3
after min, [1, 2, 3], become: 1
after min?, [1, 2, 3], become: 1
after minmax, [1, 2, 3], become: {1, 3}
after minmax?, [1, 2, 3], become: {1, 3}
after none?, [1, 2, 3], become: false
after one?, [1, 2, 3], become: false
after size, [1, 2, 3], become: 3
after tally, [1, 2, 3], become: {1 => 1, 2 => 1, 3 => 1}
after to_a, [1, 2, 3], become: [1, 2, 3]
after to_a, [] of Int32, become: [] of Int32
after to_set, [1, 2, 3], become: Set{1, 2, 3}
end

it "works with String" do
after ascii_only?, "你好", become: false
after blank?, "hello_world", become: false
after bytesize, "你好", become: 6
after bytes, "你好", become: [228, 189, 160, 229, 165, 189]
after camelcase, "hello_world", become: "HelloWorld"
after capitalize, "hello_world", become: "Hello_world"
after chars, "hello_world", become: ['h', 'e', 'l', 'l', 'o', '_', 'w', 'o', 'r', 'l', 'd']
after chomp, "hello_world\r\n", become: "hello_world"
after clone, "hello_world", become: "hello_world"
after codepoints, "你好", become: [20320, 22909]
after downcase, "hEllo", become: "hello"
after empty?, "", become: true
after empty?, " ", become: false
after has_back_references?, "hel\\lo", become: 3
after hexbytes, "0102031aff", become: Bytes[1, 2, 3, 26, 255]
after hexbytes?, "0102031aff", become: Bytes[1, 2, 3, 26, 255]
after lstrip, "\rhello_world\r\n", become: "hello_world\r\n"
after presence, "hello_world", become: "hello_world"
after rchop, "hello_world\r\n", become: "hello_world\r"
after rchop?, "hello_world\r\n", become: "hello_world\r"
after reverse, "hello_world", become: "dlrow_olleh"
after rstrip, " hello_world ", become: " hello_world"
after size, "hello_world", become: 11
after split, "hello world", become: ["hello", "world"]
after squeeze, "hello world", become: "helo world"
after strip, " hello world ", become: "hello world"
after succ, "hello_world", become: "hello_worle"
after to_big_d, "1212341515125412412412421", become: BigDecimal.new("1212341515125412412412421")
after to_big_f, "1234.0", become: 1234.0
after to_big_i, "1212341515125412412412421", become: BigInt.new("1212341515125412412412421")
after to_f, "123.45e1", become: 1234.5
after to_i, "123", become: 123
after to_s, "123", become: "123"
after underscore, "HelloWorld", become: "hello_world"
after upcase, "hEllo", become: "HELLO"
after valid_encoding?, "hello_world", become: true
end

it "works With Number" do
after "-", 1, become: -1
end

it "works with NamedTuple" do
after keys, {a: 1, b: "2"}, become: {:a, :b}
after sorted_keys, {a: 1}, become: {:a}
end

it "works with Tuple" do
after first, {1, :a, "3"}, become: 1
after first?, {1, 2, 3}, become: 1
after last, {1, 2, 3}, become: 3
after last?, {1, 2, 3}, become: 3
after reverse, {1, 2, 3}, become: {3, 2, 1}
after size, {1, 2, 3}, become: 3
after to_a, {1, 2, 3}, become: [1, 2, 3]
after min, {2, 1, 3}, become: 1
after max, {2, 3, 1}, become: 3
end
end
6 changes: 0 additions & 6 deletions src/suggest.cr

This file was deleted.

17 changes: 17 additions & 0 deletions src/what_happen.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require "./what_happen/**"

# Tells you what happen from x to y
#
# ```
# what_happen(from: false, to: "false") # => ["to_s", "inspect", "pretty_inspect"]
# what_happen(from: 1, to: -1) # => ["-"]
# what_happen(from: {1, 2, 3}, to: 1) # => ["first", "first?", "min", "min?"]
# what_happen(from: {a: 1}, to: {:a}) # => ["keys", "sorted_keys"]
# what_happen(from: [1, 2], to: 1) # => ["first", "first?", "min", "min?"]
# what_happen(from: [1, 2, 3], to: 3) # => ["size", "last", "last?", "max", "max?"]
# what_happen(from: "hello", to: "HELLO") # => ["upcase"]
# what_happen(from: "Hello", to: "hello") # => ["downcase", "underscore"]
# ```
def what_happen(from, to) : Array(String)
from.what_happen?(to)
end
24 changes: 24 additions & 0 deletions src/what_happen/extension.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
abstract class Object
def what_happen?(expect) : Array(String)
array = [] of String
generate_what_happen_by_ancestors
generate_what_happen_by_self
array.uniq
end

def <=>(other)
return nil if !other.is_a?(self.class)
other <=> self
end
end

abstract struct Number
def <=>(other) : Int32?
return nil if !other.is_a?(Number) # Crystal did not limit the type of `other` in Number
# NaN can't be compared to other numbers
return nil if self.is_a?(Float) && self.nan?
return nil if other.is_a?(Float) && other.nan?

self > other ? 1 : (self < other ? -1 : 0)
end
end
27 changes: 27 additions & 0 deletions src/what_happen/macro.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module WhatHappen
SKIP_METHODS = ["transpose", "to_h", "sample", "sum", "product"]
end

macro generate_what_happen_by_self
{% begin %}
{% methods = @type.methods.select { |x| (x.args.size == 0 || x.args.all? { |c| !c.default_value.is_a?(Nop) || c.name == "" }) && !x.accepts_block? }.map(&.name) %}
{% for m in methods %}
{% if !WhatHappen::SKIP_METHODS.includes?(m.stringify) %}
array << {{ m.stringify }} if expect == self.{{ m.id }} rescue false
{% end %}
{% end %}
{% end %}
end

macro generate_what_happen_by_ancestors
{% begin %}
{% for k in @type.ancestors %}
{% methods = k.methods.select { |x| (x.args.size == 0 || x.args.all? { |c| !c.default_value.is_a?(Nop) || c.name == "" }) && !x.accepts_block? }.map(&.name) %}
{% for m in methods %}
{% if !WhatHappen::SKIP_METHODS.includes?(m.stringify) %}
array << {{ m.stringify }} if expect == self.{{ m.id }} rescue false
{% end %}
{% end %}
{% end %}
{% end %}
end
3 changes: 3 additions & 0 deletions src/what_happen/version.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module WhatHappen
VERSION = "0.1.0"
end

0 comments on commit fb7ed1d

Please sign in to comment.